2014-09-19 01:28:14 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2016-01-12 22:04:55 +00:00
|
|
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
2014-09-19 01:28:14 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
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>
|
2014-09-24 20:45:58 +00:00
|
|
|
#include <list>
|
2014-09-19 01:28:14 +00:00
|
|
|
|
|
|
|
#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"
|
2014-09-24 20:45:58 +00:00
|
|
|
#include "OutboundMulticast.hpp"
|
2014-09-19 01:28:14 +00:00
|
|
|
#include "Utils.hpp"
|
2014-09-23 17:26:30 +00:00
|
|
|
#include "Mutex.hpp"
|
2014-10-01 21:05:25 +00:00
|
|
|
#include "NonCopyable.hpp"
|
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;
|
2014-09-19 01:28:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Database of known multicast peers within a network
|
|
|
|
*/
|
2014-10-01 21:05:25 +00:00
|
|
|
class Multicaster : NonCopyable
|
2014-09-19 01:28:14 +00:00
|
|
|
{
|
|
|
|
private:
|
2015-08-27 23:17:21 +00:00
|
|
|
struct Key
|
|
|
|
{
|
|
|
|
Key() : nwid(0),mg() {}
|
|
|
|
Key(uint64_t n,const MulticastGroup &g) : nwid(n),mg(g) {}
|
|
|
|
|
|
|
|
uint64_t nwid;
|
|
|
|
MulticastGroup mg;
|
|
|
|
|
|
|
|
inline bool operator==(const Key &k) const throw() { return ((nwid == k.nwid)&&(mg == k.mg)); }
|
|
|
|
inline unsigned long hashCode() const throw() { return (mg.hashCode() ^ (unsigned long)(nwid ^ (nwid >> 32))); }
|
|
|
|
};
|
|
|
|
|
2014-09-19 01:28:14 +00:00
|
|
|
struct MulticastGroupMember
|
|
|
|
{
|
|
|
|
MulticastGroupMember() {}
|
2014-11-21 18:50:27 +00:00
|
|
|
MulticastGroupMember(const Address &a,uint64_t ts) : address(a),timestamp(ts) {}
|
2014-09-19 01:28:14 +00:00
|
|
|
|
|
|
|
Address address;
|
2014-11-21 18:50:27 +00:00
|
|
|
uint64_t timestamp; // time of last notification
|
2014-09-19 01:28:14 +00:00
|
|
|
};
|
|
|
|
|
2014-09-22 20:18:24 +00:00
|
|
|
struct MulticastGroupStatus
|
|
|
|
{
|
2014-11-21 18:50:27 +00:00
|
|
|
MulticastGroupStatus() : lastExplicitGather(0) {}
|
2014-09-22 20:18:24 +00:00
|
|
|
|
2014-10-04 05:30:10 +00:00
|
|
|
uint64_t lastExplicitGather;
|
2015-06-26 21:26:35 +00:00
|
|
|
std::list<OutboundMulticast> txQueue; // pending outbound multicasts
|
2014-09-25 22:08:29 +00:00
|
|
|
std::vector<MulticastGroupMember> members; // members of this group
|
2014-09-22 20:18:24 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
*/
|
2014-11-21 18:50:27 +00:00
|
|
|
inline void add(uint64_t now,uint64_t nwid,const MulticastGroup &mg,const Address &member)
|
2014-09-25 22:08:29 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_groups_m);
|
2015-08-27 23:17:21 +00:00
|
|
|
_add(now,nwid,mg,_groups[Multicaster::Key(nwid,mg)],member);
|
2014-09-25 22:08:29 +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.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2014-11-21 18:50:27 +00:00
|
|
|
void addMultiple(uint64_t now,uint64_t nwid,const MulticastGroup &mg,const void *addresses,unsigned int count,unsigned int totalKnown);
|
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
|
|
|
|
*/
|
|
|
|
void remove(uint64_t nwid,const MulticastGroup &mg,const Address &member);
|
|
|
|
|
2014-09-30 23:28:25 +00:00
|
|
|
/**
|
|
|
|
* Append gather results to a packet by choosing registered multicast recipients at random
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2014-10-04 01:42:41 +00:00
|
|
|
* @param queryingPeer Peer asking for gather (to skip in results)
|
2014-09-30 23:28:25 +00:00
|
|
|
* @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
|
|
|
|
*/
|
2015-11-09 17:45:43 +00:00
|
|
|
unsigned int gather(const Address &queryingPeer,uint64_t nwid,const MulticastGroup &mg,Buffer<ZT_PROTO_MAX_PACKET_LENGTH> &appendTo,unsigned int limit) const;
|
2014-10-05 17:34:25 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-11 22:49:31 +00:00
|
|
|
* Get subscribers to a multicast group
|
2014-10-05 17:34:25 +00:00
|
|
|
*
|
|
|
|
* @param nwid Network ID
|
|
|
|
* @param mg Multicast group
|
|
|
|
*/
|
2014-10-11 22:49:31 +00:00
|
|
|
std::vector<Address> getMembers(uint64_t nwid,const MulticastGroup &mg,unsigned int limit) const;
|
2014-09-30 23:28:25 +00:00
|
|
|
|
2014-09-24 20:45:58 +00:00
|
|
|
/**
|
|
|
|
* Send a multicast
|
|
|
|
*
|
2014-09-25 22:08:29 +00:00
|
|
|
* @param limit Multicast limit
|
2014-09-24 20:45:58 +00:00
|
|
|
* @param now Current time
|
2014-10-04 20:15:02 +00:00
|
|
|
* @param nwid Network ID
|
2016-09-27 19:22:25 +00:00
|
|
|
* @param disableCompression Disable packet payload compression?
|
2014-10-04 20:15:02 +00:00
|
|
|
* @param alwaysSendTo Send to these peers first and even if not included in subscriber list
|
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
|
|
|
|
* @param data Packet data
|
|
|
|
* @param len Length of packet data
|
|
|
|
*/
|
2014-09-26 21:18:25 +00:00
|
|
|
void send(
|
|
|
|
unsigned int limit,
|
|
|
|
uint64_t now,
|
2014-09-30 23:28:25 +00:00
|
|
|
uint64_t nwid,
|
2016-09-27 19:22:25 +00:00
|
|
|
bool disableCompression,
|
2014-10-04 20:15:02 +00:00
|
|
|
const std::vector<Address> &alwaysSendTo,
|
2014-09-26 21:18:25 +00:00
|
|
|
const MulticastGroup &mg,
|
|
|
|
const MAC &src,
|
|
|
|
unsigned int etherType,
|
|
|
|
const void *data,
|
|
|
|
unsigned int len);
|
2014-09-19 01:28:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up and resort database
|
|
|
|
*
|
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
|
|
|
*/
|
2014-10-01 21:05:25 +00:00
|
|
|
void clean(uint64_t now);
|
2014-09-19 01:28:14 +00:00
|
|
|
|
2016-09-09 18:36:10 +00:00
|
|
|
/**
|
|
|
|
* Add an authorization credential
|
|
|
|
*
|
|
|
|
* The Multicaster keeps its own track of when valid credentials of network
|
|
|
|
* membership are presented. This allows it to control MULTICAST_LIKE
|
|
|
|
* GATHER authorization for networks this node does not belong to.
|
|
|
|
*
|
|
|
|
* @param com Certificate of membership
|
|
|
|
* @param alreadyValidated If true, COM has already been checked and found to be valid and signed
|
|
|
|
*/
|
|
|
|
void addCredential(const CertificateOfMembership &com,bool alreadyValidated);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check authorization for GATHER and LIKE for non-network-members
|
|
|
|
*
|
|
|
|
* @param a Address of peer
|
|
|
|
* @param nwid Network ID
|
|
|
|
* @param now Current time
|
|
|
|
* @return True if GATHER and LIKE should be allowed
|
|
|
|
*/
|
|
|
|
bool cacheAuthorized(const Address &a,const uint64_t nwid,const uint64_t now) const
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_gatherAuth_m);
|
|
|
|
const uint64_t *p = _gatherAuth.get(_GatherAuthKey(nwid,a));
|
|
|
|
return ((p)&&((now - *p) < ZT_MULTICAST_CREDENTIAL_EXPIRATON));
|
|
|
|
}
|
|
|
|
|
2014-09-19 01:28:14 +00:00
|
|
|
private:
|
2014-11-21 18:50:27 +00:00
|
|
|
void _add(uint64_t now,uint64_t nwid,const MulticastGroup &mg,MulticastGroupStatus &gs,const Address &member);
|
2014-09-25 22:08:29 +00:00
|
|
|
|
2014-10-01 21:05:25 +00:00
|
|
|
const RuntimeEnvironment *RR;
|
2016-09-09 18:36:10 +00:00
|
|
|
|
2015-08-27 23:17:21 +00:00
|
|
|
Hashtable<Multicaster::Key,MulticastGroupStatus> _groups;
|
2014-09-23 17:26:30 +00:00
|
|
|
Mutex _groups_m;
|
2016-09-09 18:36:10 +00:00
|
|
|
|
|
|
|
struct _GatherAuthKey
|
|
|
|
{
|
|
|
|
_GatherAuthKey() : member(0),networkId(0) {}
|
|
|
|
_GatherAuthKey(const uint64_t nwid,const Address &a) : member(a.toInt()),networkId(nwid) {}
|
|
|
|
inline unsigned long hashCode() const { return (member ^ networkId); }
|
|
|
|
inline bool operator==(const _GatherAuthKey &k) const { return ((member == k.member)&&(networkId == k.networkId)); }
|
|
|
|
uint64_t member;
|
|
|
|
uint64_t networkId;
|
|
|
|
};
|
|
|
|
Hashtable< _GatherAuthKey,uint64_t > _gatherAuth;
|
|
|
|
Mutex _gatherAuth_m;
|
2014-09-19 01:28:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|