A bunch of multicast work... in progress.
This commit is contained in:
parent
0d2c87fe4e
commit
540ee69773
11 changed files with 139 additions and 911 deletions
|
@ -248,7 +248,7 @@
|
|||
*
|
||||
* The protocol allows up to 7, but we limit it to something smaller.
|
||||
*/
|
||||
#define ZT_RELAY_MAX_HOPS 3
|
||||
#define ZT_RELAY_MAX_HOPS 4
|
||||
|
||||
/**
|
||||
* Expire time for multicast 'likes' and indirect multicast memberships in ms
|
||||
|
|
|
@ -16,19 +16,12 @@
|
|||
|
||||
#include "Constants.hpp"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <algorithm>
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
/**
|
||||
* A minimal hash table implementation for the ZeroTier core
|
||||
*
|
||||
* This is optimized for smaller data sets.
|
||||
*/
|
||||
template<typename K,typename V>
|
||||
class Hashtable
|
||||
|
@ -40,9 +33,9 @@ private:
|
|||
ZT_ALWAYS_INLINE _Bucket(const K &k) : k(k),v() {}
|
||||
ZT_ALWAYS_INLINE _Bucket(const _Bucket &b) : k(b.k),v(b.v) {}
|
||||
ZT_ALWAYS_INLINE _Bucket &operator=(const _Bucket &b) { k = b.k; v = b.v; return *this; }
|
||||
K k;
|
||||
V v;
|
||||
_Bucket *next; // must be set manually for each _Bucket
|
||||
const K k;
|
||||
V v;
|
||||
};
|
||||
|
||||
public:
|
||||
|
@ -95,9 +88,9 @@ public:
|
|||
//friend class Hashtable<K,V>::Iterator;
|
||||
|
||||
/**
|
||||
* @param bc Initial capacity in buckets (default: 64, must be nonzero)
|
||||
* @param bc Initial capacity in buckets (default: 32, must be nonzero)
|
||||
*/
|
||||
inline Hashtable(unsigned long bc = 64) :
|
||||
ZT_ALWAYS_INLINE Hashtable(unsigned long bc = 32) :
|
||||
_t(reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * bc))),
|
||||
_bc(bc),
|
||||
_s(0)
|
||||
|
@ -108,7 +101,7 @@ public:
|
|||
_t[i] = (_Bucket *)0;
|
||||
}
|
||||
|
||||
inline Hashtable(const Hashtable<K,V> &ht) :
|
||||
ZT_ALWAYS_INLINE Hashtable(const Hashtable<K,V> &ht) :
|
||||
_t(reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * ht._bc))),
|
||||
_bc(ht._bc),
|
||||
_s(ht._s)
|
||||
|
@ -128,13 +121,13 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
inline ~Hashtable()
|
||||
ZT_ALWAYS_INLINE ~Hashtable()
|
||||
{
|
||||
this->clear();
|
||||
::free(_t);
|
||||
}
|
||||
|
||||
inline Hashtable &operator=(const Hashtable<K,V> &ht)
|
||||
ZT_ALWAYS_INLINE Hashtable &operator=(const Hashtable<K,V> &ht)
|
||||
{
|
||||
this->clear();
|
||||
if (ht._s) {
|
||||
|
@ -152,7 +145,7 @@ public:
|
|||
/**
|
||||
* Erase all entries
|
||||
*/
|
||||
inline void clear()
|
||||
ZT_ALWAYS_INLINE void clear()
|
||||
{
|
||||
if (_s) {
|
||||
for(unsigned long i=0;i<_bc;++i) {
|
||||
|
@ -171,7 +164,7 @@ public:
|
|||
/**
|
||||
* @return Vector of all keys
|
||||
*/
|
||||
inline typename std::vector<K> keys() const
|
||||
ZT_ALWAYS_INLINE typename std::vector<K> keys() const
|
||||
{
|
||||
typename std::vector<K> k;
|
||||
if (_s) {
|
||||
|
@ -194,7 +187,7 @@ public:
|
|||
* @tparam Type of V (generally inferred)
|
||||
*/
|
||||
template<typename C>
|
||||
inline void appendKeys(C &v) const
|
||||
ZT_ALWAYS_INLINE void appendKeys(C &v) const
|
||||
{
|
||||
if (_s) {
|
||||
for(unsigned long i=0;i<_bc;++i) {
|
||||
|
@ -210,7 +203,7 @@ public:
|
|||
/**
|
||||
* @return Vector of all entries (pairs of K,V)
|
||||
*/
|
||||
inline typename std::vector< std::pair<K,V> > entries() const
|
||||
ZT_ALWAYS_INLINE typename std::vector< std::pair<K,V> > entries() const
|
||||
{
|
||||
typename std::vector< std::pair<K,V> > k;
|
||||
if (_s) {
|
||||
|
@ -230,7 +223,7 @@ public:
|
|||
* @param k Key
|
||||
* @return Pointer to value or NULL if not found
|
||||
*/
|
||||
ZT_ALWAYS_INLINE V *get(const K &k)
|
||||
ZT_ALWAYS_INLINE V *get(const K k)
|
||||
{
|
||||
_Bucket *b = _t[_hc(k) % _bc];
|
||||
while (b) {
|
||||
|
@ -240,7 +233,7 @@ public:
|
|||
}
|
||||
return (V *)0;
|
||||
}
|
||||
inline const V *get(const K &k) const { return const_cast<Hashtable *>(this)->get(k); }
|
||||
ZT_ALWAYS_INLINE const V *get(const K k) const { return const_cast<Hashtable *>(this)->get(k); }
|
||||
|
||||
/**
|
||||
* @param k Key
|
||||
|
@ -279,7 +272,7 @@ public:
|
|||
* @param k Key
|
||||
* @return True if value was present
|
||||
*/
|
||||
inline bool erase(const K &k)
|
||||
ZT_ALWAYS_INLINE bool erase(const K &k)
|
||||
{
|
||||
const unsigned long bidx = _hc(k) % _bc;
|
||||
_Bucket *lastb = (_Bucket *)0;
|
||||
|
@ -304,7 +297,7 @@ public:
|
|||
* @param v Value
|
||||
* @return Reference to value in table
|
||||
*/
|
||||
inline V &set(const K &k,const V &v)
|
||||
ZT_ALWAYS_INLINE V &set(const K &k,const V &v)
|
||||
{
|
||||
const unsigned long h = _hc(k);
|
||||
unsigned long bidx = h % _bc;
|
||||
|
@ -334,7 +327,7 @@ public:
|
|||
* @param k Key
|
||||
* @return Value, possibly newly created
|
||||
*/
|
||||
inline V &operator[](const K &k)
|
||||
ZT_ALWAYS_INLINE V &operator[](const K k)
|
||||
{
|
||||
const unsigned long h = _hc(k);
|
||||
unsigned long bidx = h % _bc;
|
||||
|
@ -379,7 +372,7 @@ private:
|
|||
static ZT_ALWAYS_INLINE unsigned long _hc(void *p) { return ((unsigned long)((uintptr_t)p) * (unsigned long)0x9e3779b1); }
|
||||
static ZT_ALWAYS_INLINE unsigned long _hc(const void *p) { return ((unsigned long)((uintptr_t)p) * (unsigned long)0x9e3779b1); }
|
||||
|
||||
inline void _grow()
|
||||
ZT_ALWAYS_INLINE void _grow()
|
||||
{
|
||||
const unsigned long nc = _bc * 2;
|
||||
_Bucket **nt = reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * nc));
|
||||
|
|
|
@ -260,6 +260,27 @@ public:
|
|||
*/
|
||||
ZT_ALWAYS_INLINE const Address &address() const { return _address; }
|
||||
|
||||
/**
|
||||
* Attempt to generate an older type identity from a newer type
|
||||
*
|
||||
* If this identity has its private key this is not transferred to
|
||||
* the downgraded identity.
|
||||
*
|
||||
* @param dest Destination to fill with downgraded identity
|
||||
* @param toType Desired identity type
|
||||
*/
|
||||
inline bool downgrade(Identity &dest,const Type toType)
|
||||
{
|
||||
if ((_type == P384)&&(toType == C25519)) {
|
||||
dest._address = _address;
|
||||
dest._type = C25519;
|
||||
dest._hasPrivate = false;
|
||||
memcpy(dest._pub.c25519,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize this identity (binary)
|
||||
*
|
||||
|
|
|
@ -29,120 +29,39 @@ namespace ZeroTier {
|
|||
|
||||
Multicaster::Multicaster(const RuntimeEnvironment *renv) :
|
||||
RR(renv),
|
||||
_groups(32)
|
||||
_groups(32) {}
|
||||
|
||||
Multicaster::~Multicaster() {}
|
||||
|
||||
void Multicaster::add(const int64_t now,const uint64_t nwid,const MulticastGroup &mg,const Address &member)
|
||||
{
|
||||
Mutex::Lock l(_groups_l);
|
||||
_groups[Multicaster::Key(nwid,mg)].set(member,now);
|
||||
}
|
||||
|
||||
Multicaster::~Multicaster()
|
||||
void Multicaster::addMultiple(const int64_t now,const uint64_t nwid,const MulticastGroup &mg,const void *addresses,unsigned int count,const unsigned int totalKnown)
|
||||
{
|
||||
}
|
||||
|
||||
void Multicaster::addMultiple(void *tPtr,int64_t now,uint64_t nwid,const MulticastGroup &mg,const void *addresses,unsigned int count,unsigned int totalKnown)
|
||||
{
|
||||
const unsigned char *p = (const unsigned char *)addresses;
|
||||
const unsigned char *e = p + (5 * count);
|
||||
Mutex::Lock _l(_groups_m);
|
||||
MulticastGroupStatus &gs = _groups[Multicaster::Key(nwid,mg)];
|
||||
while (p != e) {
|
||||
_add(tPtr,now,nwid,mg,gs,Address(p,5));
|
||||
p += 5;
|
||||
Mutex::Lock l(_groups_l);
|
||||
const uint8_t *a = (const uint8_t *)addresses;
|
||||
Hashtable< Address,int64_t > &members = _groups[Multicaster::Key(nwid,mg)];
|
||||
while (count--) {
|
||||
members.set(Address(a,ZT_ADDRESS_LENGTH),now);
|
||||
a += ZT_ADDRESS_LENGTH;
|
||||
}
|
||||
}
|
||||
|
||||
void Multicaster::remove(uint64_t nwid,const MulticastGroup &mg,const Address &member)
|
||||
void Multicaster::remove(const uint64_t nwid,const MulticastGroup &mg,const Address &member)
|
||||
{
|
||||
Mutex::Lock _l(_groups_m);
|
||||
MulticastGroupStatus *s = _groups.get(Multicaster::Key(nwid,mg));
|
||||
if (s) {
|
||||
for(std::vector<MulticastGroupMember>::iterator m(s->members.begin());m!=s->members.end();++m) {
|
||||
if (m->address == member) {
|
||||
s->members.erase(m);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Mutex::Lock l(_groups_l);
|
||||
const Multicaster::Key gk(nwid,mg);
|
||||
Hashtable< Address,int64_t > *const members = _groups.get(gk);
|
||||
if (members) {
|
||||
members->erase(member);
|
||||
if (members->empty())
|
||||
_groups.erase(gk);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int Multicaster::gather(const Address &queryingPeer,uint64_t nwid,const MulticastGroup &mg,Buffer<ZT_PROTO_MAX_PACKET_LENGTH> &appendTo,unsigned int limit) const
|
||||
{
|
||||
unsigned char *p;
|
||||
unsigned int added = 0,i,k,rptr,totalKnown = 0;
|
||||
uint64_t a,picked[(ZT_PROTO_MAX_PACKET_LENGTH / 5) + 2];
|
||||
|
||||
if (!limit)
|
||||
return 0;
|
||||
else if (limit > 0xffff)
|
||||
limit = 0xffff;
|
||||
|
||||
const unsigned int totalAt = appendTo.size();
|
||||
appendTo.addSize(4); // sizeof(uint32_t)
|
||||
const unsigned int addedAt = appendTo.size();
|
||||
appendTo.addSize(2); // sizeof(uint16_t)
|
||||
|
||||
{ // Return myself if I am a member of this group
|
||||
SharedPtr<Network> network(RR->node->network(nwid));
|
||||
if ((network)&&(network->subscribedToMulticastGroup(mg,true))) {
|
||||
RR->identity.address().appendTo(appendTo);
|
||||
++totalKnown;
|
||||
++added;
|
||||
}
|
||||
}
|
||||
|
||||
Mutex::Lock _l(_groups_m);
|
||||
|
||||
const MulticastGroupStatus *s = _groups.get(Multicaster::Key(nwid,mg));
|
||||
if ((s)&&(!s->members.empty())) {
|
||||
totalKnown += (unsigned int)s->members.size();
|
||||
|
||||
// Members are returned in random order so that repeated gather queries
|
||||
// will return different subsets of a large multicast group.
|
||||
k = 0;
|
||||
while ((added < limit)&&(k < s->members.size())&&((appendTo.size() + ZT_ADDRESS_LENGTH) <= ZT_PROTO_MAX_PACKET_LENGTH)) {
|
||||
rptr = (unsigned int)Utils::random();
|
||||
|
||||
restart_member_scan:
|
||||
a = s->members[rptr % (unsigned int)s->members.size()].address.toInt();
|
||||
for(i=0;i<k;++i) {
|
||||
if (picked[i] == a) {
|
||||
++rptr;
|
||||
goto restart_member_scan;
|
||||
}
|
||||
}
|
||||
picked[k++] = a;
|
||||
|
||||
if (queryingPeer.toInt() != a) { // do not return the peer that is making the request as a result
|
||||
p = (unsigned char *)appendTo.appendField(ZT_ADDRESS_LENGTH);
|
||||
*(p++) = (unsigned char)((a >> 32) & 0xff);
|
||||
*(p++) = (unsigned char)((a >> 24) & 0xff);
|
||||
*(p++) = (unsigned char)((a >> 16) & 0xff);
|
||||
*(p++) = (unsigned char)((a >> 8) & 0xff);
|
||||
*p = (unsigned char)(a & 0xff);
|
||||
++added;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
appendTo.setAt(totalAt,(uint32_t)totalKnown);
|
||||
appendTo.setAt(addedAt,(uint16_t)added);
|
||||
|
||||
return added;
|
||||
}
|
||||
|
||||
std::vector<Address> Multicaster::getMembers(uint64_t nwid,const MulticastGroup &mg,unsigned int limit) const
|
||||
{
|
||||
std::vector<Address> ls;
|
||||
Mutex::Lock _l(_groups_m);
|
||||
const MulticastGroupStatus *s = _groups.get(Multicaster::Key(nwid,mg));
|
||||
if (!s)
|
||||
return ls;
|
||||
for(std::vector<MulticastGroupMember>::const_reverse_iterator m(s->members.rbegin());m!=s->members.rend();++m) {
|
||||
ls.push_back(m->address);
|
||||
if (ls.size() >= limit)
|
||||
break;
|
||||
}
|
||||
return ls;
|
||||
}
|
||||
|
||||
void Multicaster::send(
|
||||
void *tPtr,
|
||||
int64_t now,
|
||||
|
@ -154,6 +73,7 @@ void Multicaster::send(
|
|||
const void *data,
|
||||
unsigned int len)
|
||||
{
|
||||
#if 0
|
||||
unsigned long idxbuf[4096];
|
||||
unsigned long *indexes = idxbuf;
|
||||
|
||||
|
@ -322,10 +242,12 @@ void Multicaster::send(
|
|||
// Free allocated memory buffer if any
|
||||
if (indexes != idxbuf)
|
||||
delete [] indexes;
|
||||
#endif
|
||||
}
|
||||
|
||||
void Multicaster::clean(int64_t now)
|
||||
{
|
||||
#if 0
|
||||
{
|
||||
Mutex::Lock _l(_groups_m);
|
||||
Multicaster::Key *k = (Multicaster::Key *)0;
|
||||
|
@ -361,37 +283,7 @@ void Multicaster::clean(int64_t now)
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Multicaster::_add(void *tPtr,int64_t now,uint64_t nwid,const MulticastGroup &mg,MulticastGroupStatus &gs,const Address &member)
|
||||
{
|
||||
// assumes _groups_m is locked
|
||||
|
||||
// Do not add self -- even if someone else returns it
|
||||
if (member == RR->identity.address())
|
||||
return;
|
||||
|
||||
std::vector<MulticastGroupMember>::iterator m(std::lower_bound(gs.members.begin(),gs.members.end(),member));
|
||||
if (m != gs.members.end()) {
|
||||
if (m->address == member) {
|
||||
m->timestamp = now;
|
||||
return;
|
||||
}
|
||||
gs.members.insert(m,MulticastGroupMember(member,now));
|
||||
} else {
|
||||
gs.members.push_back(MulticastGroupMember(member,now));
|
||||
}
|
||||
|
||||
for(std::list<OutboundMulticast>::iterator tx(gs.txQueue.begin());tx!=gs.txQueue.end();) {
|
||||
if (tx->atLimit())
|
||||
gs.txQueue.erase(tx++);
|
||||
else {
|
||||
tx->sendIfNew(RR,tPtr,member);
|
||||
if (tx->atLimit())
|
||||
gs.txQueue.erase(tx++);
|
||||
else ++tx;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
#include "Constants.hpp"
|
||||
#include "Hashtable.hpp"
|
||||
|
@ -39,7 +38,7 @@ class Packet;
|
|||
class Network;
|
||||
|
||||
/**
|
||||
* Database of known multicast peers within a network
|
||||
* Multicast database and outbound multicast handler
|
||||
*/
|
||||
class Multicaster
|
||||
{
|
||||
|
@ -55,11 +54,7 @@ public:
|
|||
* @param mg Multicast group
|
||||
* @param member New member address
|
||||
*/
|
||||
inline void add(void *tPtr,int64_t now,uint64_t nwid,const MulticastGroup &mg,const Address &member)
|
||||
{
|
||||
Mutex::Lock _l(_groups_m);
|
||||
_add(tPtr,now,nwid,mg,_groups[Multicaster::Key(nwid,mg)],member);
|
||||
}
|
||||
void add(const int64_t now,const uint64_t nwid,const MulticastGroup &mg,const Address &member);
|
||||
|
||||
/**
|
||||
* Add multiple addresses from a binary array of 5-byte address fields
|
||||
|
@ -74,7 +69,7 @@ public:
|
|||
* @param count Number of addresses
|
||||
* @param totalKnown Total number of known addresses as reported by peer
|
||||
*/
|
||||
void addMultiple(void *tPtr,int64_t now,uint64_t nwid,const MulticastGroup &mg,const void *addresses,unsigned int count,unsigned int totalKnown);
|
||||
void addMultiple(const int64_t now,const uint64_t nwid,const MulticastGroup &mg,const void *addresses,unsigned int count,const unsigned int totalKnown);
|
||||
|
||||
/**
|
||||
* Remove a multicast group member (if present)
|
||||
|
@ -83,35 +78,46 @@ public:
|
|||
* @param mg Multicast group
|
||||
* @param member Member to unsubscribe
|
||||
*/
|
||||
void remove(uint64_t nwid,const MulticastGroup &mg,const Address &member);
|
||||
void remove(const uint64_t nwid,const MulticastGroup &mg,const Address &member);
|
||||
|
||||
/**
|
||||
* Append gather results to a packet by choosing registered multicast recipients at random
|
||||
* Iterate over members of a multicast group until function returns false
|
||||
*
|
||||
* This appends the following fields to the packet:
|
||||
* <[4] 32-bit total number of known members in this multicast group>
|
||||
* <[2] 16-bit number of members enumerated in this packet>
|
||||
* <[...] series of 5-byte ZeroTier addresses of enumerated members>
|
||||
*
|
||||
* If zero is returned, the first two fields will still have been appended.
|
||||
*
|
||||
* @param queryingPeer Peer asking for gather (to skip in results)
|
||||
* @param nwid Network ID
|
||||
* @param mg Multicast group
|
||||
* @param appendTo Packet to append to
|
||||
* @param limit Maximum number of 5-byte addresses to append
|
||||
* @return Number of addresses appended
|
||||
* @throws std::out_of_range Buffer overflow writing to packet
|
||||
*/
|
||||
unsigned int gather(const Address &queryingPeer,uint64_t nwid,const MulticastGroup &mg,Buffer<ZT_PROTO_MAX_PACKET_LENGTH> &appendTo,unsigned int limit) const;
|
||||
|
||||
/**
|
||||
* Get subscribers to a multicast group
|
||||
* Iteration order is in inverse order of most recent receipt of a LIKE
|
||||
* for a given membership.
|
||||
*
|
||||
* @param nwid Network ID
|
||||
* @param mg Multicast group
|
||||
* @param func f(Address)
|
||||
* @return Total number of known members (regardless of when function aborted)
|
||||
*/
|
||||
std::vector<Address> getMembers(uint64_t nwid,const MulticastGroup &mg,unsigned int limit) const;
|
||||
template<typename F>
|
||||
inline unsigned long eachMember(const uint64_t nwid,const MulticastGroup &mg,F func) const
|
||||
{
|
||||
std::vector< std::pair<int64_t,Address> > sortedByTime;
|
||||
{
|
||||
Mutex::Lock l(_groups_l);
|
||||
const Multicaster::Key gk(nwid,mg);
|
||||
const Hashtable< Address,int64_t > *const members = _groups.get(gk);
|
||||
if (members) {
|
||||
totalKnown = members->size();
|
||||
sortedByTime.reserve(totalKnown);
|
||||
{
|
||||
Hashtable< Address,int64_t >::Iterator mi(*const_cast<Hashtable< Address,int64_t > *>(members));
|
||||
Address *mik = nullptr;
|
||||
int64_t *miv = nullptr;
|
||||
while (mi.next(mik,miv))
|
||||
sortedByTime.push_back(std::pair<int64_t,Address>(*miv,*mik));
|
||||
}
|
||||
std::sort(sortedByTime.begin(),sortedByTime.end());
|
||||
}
|
||||
}
|
||||
for(std::vector< std::pair<int64_t,Address> >::const_reverse_iterator i(sortedByTime.begin());i!=sortedByTime.end();++i) {
|
||||
if (!func(i->second))
|
||||
break;
|
||||
}
|
||||
return sortedByTime.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a multicast
|
||||
|
@ -148,48 +154,26 @@ public:
|
|||
private:
|
||||
struct Key
|
||||
{
|
||||
inline Key() : nwid(0),mg() {}
|
||||
inline Key(uint64_t n,const MulticastGroup &g) : nwid(n),mg(g) {}
|
||||
ZT_ALWAYS_INLINE Key() : nwid(0),mg() {}
|
||||
ZT_ALWAYS_INLINE Key(const uint64_t n,const MulticastGroup &g) : nwid(n),mg(g) {}
|
||||
|
||||
uint64_t nwid;
|
||||
MulticastGroup mg;
|
||||
|
||||
inline bool operator==(const Key &k) const { return ((nwid == k.nwid)&&(mg == k.mg)); }
|
||||
inline bool operator!=(const Key &k) const { return ((nwid != k.nwid)||(mg != k.mg)); }
|
||||
inline unsigned long hashCode() const { return (mg.hashCode() ^ (unsigned long)(nwid ^ (nwid >> 32))); }
|
||||
ZT_ALWAYS_INLINE bool operator==(const Key &k) const { return ((nwid == k.nwid)&&(mg == k.mg)); }
|
||||
ZT_ALWAYS_INLINE bool operator!=(const Key &k) const { return ((nwid != k.nwid)||(mg != k.mg)); }
|
||||
|
||||
ZT_ALWAYS_INLINE unsigned long hashCode() const { return (mg.hashCode() ^ (unsigned long)(nwid ^ (nwid >> 32))); }
|
||||
};
|
||||
|
||||
struct MulticastGroupMember
|
||||
{
|
||||
inline MulticastGroupMember() {}
|
||||
inline MulticastGroupMember(const Address &a,uint64_t ts) : address(a),timestamp(ts) {}
|
||||
|
||||
inline bool operator<(const MulticastGroupMember &a) const { return (address < a.address); }
|
||||
inline bool operator==(const MulticastGroupMember &a) const { return (address == a.address); }
|
||||
inline bool operator!=(const MulticastGroupMember &a) const { return (address != a.address); }
|
||||
inline bool operator<(const Address &a) const { return (address < a); }
|
||||
inline bool operator==(const Address &a) const { return (address == a); }
|
||||
inline bool operator!=(const Address &a) const { return (address != a); }
|
||||
|
||||
Address address;
|
||||
uint64_t timestamp; // time of last notification
|
||||
};
|
||||
|
||||
struct MulticastGroupStatus
|
||||
{
|
||||
inline MulticastGroupStatus() : lastExplicitGather(0) {}
|
||||
|
||||
uint64_t lastExplicitGather;
|
||||
std::list<OutboundMulticast> txQueue; // pending outbound multicasts
|
||||
std::vector<MulticastGroupMember> members; // members of this group
|
||||
};
|
||||
|
||||
void _add(void *tPtr,int64_t now,uint64_t nwid,const MulticastGroup &mg,MulticastGroupStatus &gs,const Address &member);
|
||||
|
||||
const RuntimeEnvironment *const RR;
|
||||
|
||||
Hashtable<Multicaster::Key,MulticastGroupStatus> _groups;
|
||||
Mutex _groups_m;
|
||||
OutboundMulticast _txQueue[ZT_TX_QUEUE_SIZE];
|
||||
unsigned int _txQueuePtr;
|
||||
Mutex _txQueue_l;
|
||||
|
||||
Hashtable< Multicaster::Key,Hashtable< Address,int64_t > > _groups;
|
||||
Mutex _groups_l;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
|
|
@ -27,8 +27,6 @@ void OutboundMulticast::init(
|
|||
uint64_t timestamp,
|
||||
uint64_t nwid,
|
||||
bool disableCompression,
|
||||
unsigned int limit,
|
||||
unsigned int gatherLimit,
|
||||
const MAC &src,
|
||||
const MulticastGroup &dest,
|
||||
unsigned int etherType,
|
||||
|
@ -46,17 +44,13 @@ void OutboundMulticast::init(
|
|||
_macSrc.fromAddress(RR->identity.address(),nwid);
|
||||
}
|
||||
_macDest = dest.mac();
|
||||
_limit = limit;
|
||||
_frameLen = (len < ZT_MAX_MTU) ? len : ZT_MAX_MTU;
|
||||
_etherType = etherType;
|
||||
|
||||
if (gatherLimit) flags |= 0x02;
|
||||
|
||||
_packet.setSource(RR->identity.address());
|
||||
_packet.setVerb(Packet::VERB_MULTICAST_FRAME);
|
||||
_packet.append((uint64_t)nwid);
|
||||
_packet.append(flags);
|
||||
if (gatherLimit) _packet.append((uint32_t)gatherLimit);
|
||||
if (src) src.appendTo(_packet);
|
||||
dest.mac().appendTo(_packet);
|
||||
_packet.append((uint32_t)dest.adi());
|
||||
|
|
|
@ -43,7 +43,7 @@ public:
|
|||
*
|
||||
* It must be initialized with init().
|
||||
*/
|
||||
inline OutboundMulticast() {}
|
||||
ZT_ALWAYS_INLINE OutboundMulticast() {}
|
||||
|
||||
/**
|
||||
* Initialize outbound multicast
|
||||
|
@ -53,7 +53,6 @@ public:
|
|||
* @param nwid Network ID
|
||||
* @param disableCompression Disable compression of frame payload
|
||||
* @param limit Multicast limit for desired number of packets to send
|
||||
* @param gatherLimit Number to lazily/implicitly gather with this frame or 0 for none
|
||||
* @param src Source MAC address of frame or NULL to imply compute from sender ZT address
|
||||
* @param dest Destination multicast group (MAC + ADI)
|
||||
* @param etherType 16-bit Ethernet type ID
|
||||
|
@ -66,8 +65,6 @@ public:
|
|||
uint64_t timestamp,
|
||||
uint64_t nwid,
|
||||
bool disableCompression,
|
||||
unsigned int limit,
|
||||
unsigned int gatherLimit,
|
||||
const MAC &src,
|
||||
const MulticastGroup &dest,
|
||||
unsigned int etherType,
|
||||
|
@ -77,18 +74,18 @@ public:
|
|||
/**
|
||||
* @return Multicast creation time
|
||||
*/
|
||||
inline uint64_t timestamp() const { return _timestamp; }
|
||||
ZT_ALWAYS_INLINE uint64_t timestamp() const { return _timestamp; }
|
||||
|
||||
/**
|
||||
* @param now Current time
|
||||
* @return True if this multicast is expired (has exceeded transmit timeout)
|
||||
*/
|
||||
inline bool expired(int64_t now) const { return ((now - _timestamp) >= ZT_MULTICAST_TRANSMIT_TIMEOUT); }
|
||||
ZT_ALWAYS_INLINE bool expired(int64_t now) const { return ((now - _timestamp) >= ZT_MULTICAST_TRANSMIT_TIMEOUT); }
|
||||
|
||||
/**
|
||||
* @return True if this outbound multicast has been sent to enough peers
|
||||
*/
|
||||
inline bool atLimit() const { return (_alreadySentTo.size() >= _limit); }
|
||||
ZT_ALWAYS_INLINE bool atLimit() const { return (_alreadySentTo.size() >= _limit); }
|
||||
|
||||
/**
|
||||
* Just send without checking log
|
||||
|
@ -106,9 +103,9 @@ public:
|
|||
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
||||
* @param toAddr Destination address
|
||||
*/
|
||||
inline void sendAndLog(const RuntimeEnvironment *RR,void *tPtr,const Address &toAddr)
|
||||
ZT_ALWAYS_INLINE void sendAndLog(const RuntimeEnvironment *RR,void *tPtr,const Address &toAddr)
|
||||
{
|
||||
_alreadySentTo.push_back(toAddr);
|
||||
_alreadySentTo.insert(std::upper_bound(_alreadySentTo.begin(),_alreadySentTo.end(),toAddr),toAddr); // sorted insert
|
||||
sendOnly(RR,tPtr,toAddr);
|
||||
}
|
||||
|
||||
|
@ -117,9 +114,9 @@ public:
|
|||
*
|
||||
* @param toAddr Address to log as sent
|
||||
*/
|
||||
inline void logAsSent(const Address &toAddr)
|
||||
ZT_ALWAYS_INLINE void logAsSent(const Address &toAddr)
|
||||
{
|
||||
_alreadySentTo.push_back(toAddr);
|
||||
_alreadySentTo.insert(std::upper_bound(_alreadySentTo.begin(),_alreadySentTo.end(),toAddr),toAddr); // sorted insert
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -130,9 +127,9 @@ public:
|
|||
* @param toAddr Destination address
|
||||
* @return True if address is new and packet was sent to switch, false if duplicate
|
||||
*/
|
||||
inline bool sendIfNew(const RuntimeEnvironment *RR,void *tPtr,const Address &toAddr)
|
||||
ZT_ALWAYS_INLINE bool sendIfNew(const RuntimeEnvironment *RR,void *tPtr,const Address &toAddr)
|
||||
{
|
||||
if (std::find(_alreadySentTo.begin(),_alreadySentTo.end(),toAddr) == _alreadySentTo.end()) {
|
||||
if (!std::binary_search(_alreadySentTo.begin(),_alreadySentTo.end(),toAddr)) {
|
||||
sendAndLog(RR,tPtr,toAddr);
|
||||
return true;
|
||||
} else {
|
||||
|
@ -145,7 +142,6 @@ private:
|
|||
uint64_t _nwid;
|
||||
MAC _macSrc;
|
||||
MAC _macDest;
|
||||
unsigned int _limit;
|
||||
unsigned int _frameLen;
|
||||
unsigned int _etherType;
|
||||
Packet _packet,_tmp;
|
||||
|
|
|
@ -62,7 +62,6 @@
|
|||
* + Old planet/moon stuff is DEAD!
|
||||
* + AES-256-GMAC-CTR encryption is now the default
|
||||
* + NIST P-384 (type 1) identities now supported
|
||||
* + Minimum proto version is now 8 (1.1.17 and newer)
|
||||
* + WILL_RELAY allows mesh-like operation
|
||||
* + Ephemeral keys are now negotiated opportunistically
|
||||
*/
|
||||
|
@ -71,7 +70,7 @@
|
|||
/**
|
||||
* Minimum supported protocol version
|
||||
*/
|
||||
#define ZT_PROTO_VERSION_MIN 8
|
||||
#define ZT_PROTO_VERSION_MIN 6
|
||||
|
||||
/**
|
||||
* Maximum hop count allowed by packet structure (3 bits, 0-7)
|
||||
|
@ -93,21 +92,16 @@
|
|||
#define ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012 1
|
||||
|
||||
/**
|
||||
* Cipher suite: NONE
|
||||
* No encryption or authentication at all
|
||||
*
|
||||
* This differs from POLY1305/NONE in that *no* crypto is done, not even
|
||||
* authentication. This is for trusted local LAN interconnects for internal
|
||||
* SDN use within a data center.
|
||||
*
|
||||
* For this mode the MAC field becomes a trusted path ID and must match the
|
||||
* configured ID of a trusted path or the packet is discarded.
|
||||
* For trusted paths the MAC field is the trusted path ID.
|
||||
*/
|
||||
#define ZT_PROTO_CIPHER_SUITE__NO_CRYPTO_TRUSTED_PATH 2
|
||||
#define ZT_PROTO_CIPHER_SUITE__NONE 2
|
||||
|
||||
/**
|
||||
* AES-256-GMAC-CTR
|
||||
* AES-GMAC_SIV with AES-256
|
||||
*/
|
||||
#define ZT_PROTO_CIPHER_SUITE__AES256_GMAC_CTR 3
|
||||
#define ZT_PROTO_CIPHER_SUITE__AES256_GMAC_SIV 3
|
||||
|
||||
/**
|
||||
* Header flag indicating that a packet is fragmented
|
||||
|
@ -693,10 +687,6 @@ public:
|
|||
* <[6] MAC address of multicast group being queried>
|
||||
* <[4] 32-bit ADI for multicast group being queried>
|
||||
* <[4] 32-bit requested max number of multicast peers>
|
||||
* [<[...] network certificate of membership>]
|
||||
*
|
||||
* Flags:
|
||||
* 0x01 - COM is attached (DEPRECATED)
|
||||
*
|
||||
* More than one OK response can occur if the response is broken up across
|
||||
* multiple packets or if querying a clustered node.
|
||||
|
@ -718,10 +708,9 @@ public:
|
|||
* Multicast frame:
|
||||
* <[8] 64-bit network ID>
|
||||
* <[1] flags>
|
||||
* [<[4] 32-bit implicit gather limit>]
|
||||
* [<[...] network certificate of membership (DEPRECATED)>]
|
||||
* [<[4] 32-bit implicit gather limit (DEPRECATED)>]
|
||||
* [<[6] source MAC>]
|
||||
* [<[2] number of explicitly specified recipients>]
|
||||
* [<[...] series of 5-byte explicitly specified recipients>]
|
||||
* <[6] destination MAC (multicast address)>
|
||||
* <[4] 32-bit multicast ADI (multicast address extension)>
|
||||
* <[2] 16-bit ethertype>
|
||||
|
@ -733,20 +722,9 @@ public:
|
|||
* 0x04 - Source MAC is specified -- otherwise it's computed from sender
|
||||
* 0x08 - Explicit recipient list included for P2P/HS replication
|
||||
*
|
||||
* Explicit recipient lists are used for peer to peer or hub and spoke
|
||||
* replication.
|
||||
*
|
||||
* OK response payload:
|
||||
* <[8] 64-bit network ID>
|
||||
* <[6] MAC address of multicast group>
|
||||
* <[4] 32-bit ADI for multicast group>
|
||||
* <[1] flags>
|
||||
* [<[...] network certificate of membership (DEPRECATED)>]
|
||||
* [<[...] implicit gather results if flag 0x01 is set>]
|
||||
*
|
||||
* OK flags (same bits as request flags):
|
||||
* 0x01 - OK includes certificate of network membership (DEPRECATED)
|
||||
* 0x02 - OK includes implicit gather results
|
||||
* ERROR_MULTICAST_STFU is generated if a recipient no longer wishes to
|
||||
* receive these multicasts. It's essentially a source quench. Its
|
||||
* payload is:
|
||||
*
|
||||
* ERROR response payload:
|
||||
* <[8] 64-bit network ID>
|
||||
|
@ -965,7 +943,7 @@ public:
|
|||
ERROR_NETWORK_ACCESS_DENIED_ = 0x07, /* extra _ at end to avoid Windows name conflict */
|
||||
|
||||
/* Multicasts to this group are not wanted */
|
||||
ERROR_UNWANTED_MULTICAST = 0x08,
|
||||
ERROR_MULTICAST_STFU = 0x08,
|
||||
|
||||
/* Cannot deliver a forwarded ZeroTier packet (e.g. hops exceeded, no routes) */
|
||||
/* Payload: <packet ID>, <destination>, <... additional packet ID / destinations> */
|
||||
|
@ -1158,7 +1136,7 @@ public:
|
|||
*/
|
||||
ZT_ALWAYS_INLINE void setTrusted(const uint64_t tpid)
|
||||
{
|
||||
setCipher(ZT_PROTO_CIPHER_SUITE__NO_CRYPTO_TRUSTED_PATH);
|
||||
setCipher(ZT_PROTO_CIPHER_SUITE__NONE);
|
||||
setAt(ZT_PACKET_IDX_MAC,tpid);
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@
|
|||
|
||||
#define ZT_HMACSHA384_LEN 48
|
||||
|
||||
#define ZT_PROTO_KBKDF_LABEL_KEY_USE_HMAC 'H'
|
||||
#define ZT_PROTO_KBKDF_LABEL_KEY_USE_HMAC_SHA_384 'H'
|
||||
#define ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K1 '1'
|
||||
#define ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K2 '2'
|
||||
#define ZT_PROTO_KBKDF_LABEL_KEY_USE_AES_GMAC_SIV_K3 '3'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue