2014-09-19 01:28:14 +00:00
|
|
|
/*
|
|
|
|
* 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/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZT_MULTICASTTOPOLOGY_HPP
|
|
|
|
#define ZT_MULTICASTTOPOLOGY_HPP
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#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"
|
|
|
|
#include "Switch.hpp"
|
2014-09-19 01:28:14 +00:00
|
|
|
#include "Utils.hpp"
|
2014-09-23 17:26:30 +00:00
|
|
|
#include "Mutex.hpp"
|
2014-09-19 01:28:14 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
class Topology;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database of known multicast peers within a network
|
|
|
|
*/
|
|
|
|
class MulticastTopology
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
struct MulticastGroupMember
|
|
|
|
{
|
|
|
|
MulticastGroupMember() {}
|
|
|
|
MulticastGroupMember(const Address &a,const Address &lf,uint64_t ts) : address(a),learnedFrom(lf),timestamp(ts) {}
|
|
|
|
|
|
|
|
Address address;
|
|
|
|
Address learnedFrom; // NULL/0 for addresses directly learned from LIKE
|
|
|
|
uint64_t timestamp; // time of last LIKE or OK response to MULTICAST_LONELY
|
|
|
|
uint64_t rank; // used by sorting algorithm in clean()
|
|
|
|
|
|
|
|
// for sorting in ascending order of rank
|
|
|
|
inline bool operator<(const MulticastGroupMember &m) const throw() { return (rank < m.rank); }
|
|
|
|
};
|
|
|
|
|
2014-09-22 20:18:24 +00:00
|
|
|
struct MulticastGroupStatus
|
|
|
|
{
|
|
|
|
MulticastGroupStatus() : lastGatheredMembers(0) {}
|
|
|
|
|
|
|
|
uint64_t lastGatheredMembers; // time we last gathered members
|
|
|
|
std::vector<MulticastGroupMember> members; // members of this group
|
2014-09-24 20:45:58 +00:00
|
|
|
std::list<OutboundMulticast> txQueue; // pending outbound multicasts
|
2014-09-22 20:18:24 +00:00
|
|
|
};
|
|
|
|
|
2014-09-19 01:28:14 +00:00
|
|
|
public:
|
|
|
|
MulticastTopology();
|
|
|
|
~MulticastTopology();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add or update a member in a multicast group
|
|
|
|
*
|
|
|
|
* @param mg Multicast group
|
|
|
|
* @param learnedFrom Address from which we learned this member or NULL/0 Address if direct
|
2014-09-24 20:45:58 +00:00
|
|
|
* @param member New member address
|
2014-09-19 01:28:14 +00:00
|
|
|
*/
|
2014-09-24 20:45:58 +00:00
|
|
|
void add(const MulticastGroup &mg,const Address &learnedFrom,const Address &member);
|
2014-09-19 01:28:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Erase a member from a multicast group (if present)
|
|
|
|
*
|
|
|
|
* @param mg Multicast group
|
|
|
|
* @param member Member to erase
|
|
|
|
*/
|
2014-09-22 20:18:24 +00:00
|
|
|
void erase(const MulticastGroup &mg,const Address &member);
|
2014-09-19 01:28:14 +00:00
|
|
|
|
2014-09-24 20:45:58 +00:00
|
|
|
/**
|
|
|
|
* Send a multicast
|
|
|
|
*
|
|
|
|
* @param nwid Network ID
|
|
|
|
* @param now Current time
|
|
|
|
* @param sw Switch to use for sending packets
|
|
|
|
* @param self This node's address
|
|
|
|
* @param mg Multicast group
|
|
|
|
* @param from Source Ethernet MAC address
|
|
|
|
* @param etherType Ethernet frame type
|
|
|
|
* @param data Packet data
|
|
|
|
* @param len Length of packet data
|
|
|
|
*/
|
|
|
|
void send(uint64_t nwid,uint64_t now,const Switch &sw,const Address &self,const MulticastGroup &mg,const MAC &from,unsigned int etherType,const void *data,unsigned int len);
|
|
|
|
|
2014-09-19 01:28:14 +00:00
|
|
|
/**
|
|
|
|
* @param mg Multicast group
|
2014-09-22 20:18:24 +00:00
|
|
|
* @return Tuple of: time we last gathered members (or 0 for never) and number of known members
|
2014-09-19 01:28:14 +00:00
|
|
|
*/
|
2014-09-22 20:18:24 +00:00
|
|
|
inline std::pair<uint64_t,unsigned int> groupStatus(const MulticastGroup &mg) const
|
2014-09-19 01:28:14 +00:00
|
|
|
{
|
2014-09-23 17:26:30 +00:00
|
|
|
Mutex::Lock _l(_groups_m);
|
2014-09-22 20:18:24 +00:00
|
|
|
std::map< MulticastGroup,MulticastGroupStatus >::const_iterator r(_groups.find(mg));
|
|
|
|
return ((r != _groups.end()) ? std::pair<uint64_t,unsigned int>(r->second.lastGatheredMembers,r->second.members.size()) : std::pair<uint64_t,unsigned int>(0,0));
|
2014-09-19 01:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-22 20:18:24 +00:00
|
|
|
* Return the number of new members we should want to gather or 0 for none
|
|
|
|
*
|
|
|
|
* @param mg Multicast group
|
|
|
|
* @param now Current time
|
|
|
|
* @param limit The maximum number we want per multicast group on this network
|
|
|
|
* @param updateLastGatheredTimeOnNonzeroReturn If true, reset group's last gathered time to 'now' on non-zero return
|
|
|
|
*/
|
2014-09-24 16:01:58 +00:00
|
|
|
unsigned int shouldGather(const MulticastGroup &mg,uint64_t now,unsigned int limit,bool updateLastGatheredTimeOnNonzeroReturn);
|
2014-09-22 20:18:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update last gathered members time for a group
|
2014-09-19 01:28:14 +00:00
|
|
|
*
|
|
|
|
* @param mg Multicast group
|
2014-09-22 20:18:24 +00:00
|
|
|
* @param now Current time
|
2014-09-19 01:28:14 +00:00
|
|
|
*/
|
2014-09-22 20:18:24 +00:00
|
|
|
inline void gatheringMembersNow(const MulticastGroup &mg,uint64_t now)
|
2014-09-19 01:28:14 +00:00
|
|
|
{
|
2014-09-23 17:26:30 +00:00
|
|
|
Mutex::Lock _l(_groups_m);
|
2014-09-22 20:18:24 +00:00
|
|
|
_groups[mg].lastGatheredMembers = now;
|
2014-09-19 01:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up and resort database
|
|
|
|
*
|
2014-09-22 20:18:24 +00:00
|
|
|
* @param now Current time
|
2014-09-19 01:28:14 +00:00
|
|
|
* @param topology Global peer topology
|
2014-09-22 20:18:24 +00:00
|
|
|
* @param trim Trim lists to a maximum of this many members per multicast group
|
2014-09-19 01:28:14 +00:00
|
|
|
*/
|
2014-09-22 20:18:24 +00:00
|
|
|
void clean(uint64_t now,const Topology &topology);
|
2014-09-19 01:28:14 +00:00
|
|
|
|
|
|
|
private:
|
2014-09-22 20:18:24 +00:00
|
|
|
std::map< MulticastGroup,MulticastGroupStatus > _groups;
|
2014-09-23 17:26:30 +00:00
|
|
|
Mutex _groups_m;
|
2014-09-19 01:28:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|