mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2024-12-21 05:53:09 +00:00
140 lines
4.8 KiB
C++
140 lines
4.8 KiB
C++
/*
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
|
*
|
|
* Use of this software is governed by the Business Source License included
|
|
* in the LICENSE.TXT file in the project's root directory.
|
|
*
|
|
* Change Date: 2023-01-01
|
|
*
|
|
* 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.
|
|
*/
|
|
/****/
|
|
|
|
#ifndef ZT_MULTICASTGROUP_HPP
|
|
#define ZT_MULTICASTGROUP_HPP
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "Constants.hpp"
|
|
#include "MAC.hpp"
|
|
#include "InetAddress.hpp"
|
|
#include "Utils.hpp"
|
|
|
|
namespace ZeroTier {
|
|
|
|
/**
|
|
* A multicast group composed of a multicast MAC and a 32-bit ADI field
|
|
*
|
|
* ADI stands for additional distinguishing information. ADI is primarily for
|
|
* adding additional information to broadcast (ff:ff:ff:ff:ff:ff) memberships,
|
|
* since straight-up broadcast won't scale. Right now it's zero except for
|
|
* IPv4 ARP, where it holds the IPv4 address itself to make ARP into a
|
|
* selective multicast query that can scale.
|
|
*
|
|
* In the future we might add some kind of plugin architecture that can add
|
|
* ADI for things like mDNS (multicast DNS) to improve the selectivity of
|
|
* those protocols.
|
|
*
|
|
* MulticastGroup behaves as an immutable value object.
|
|
*/
|
|
class MulticastGroup
|
|
{
|
|
public:
|
|
ZT_ALWAYS_INLINE MulticastGroup() :
|
|
_mac(),
|
|
_adi(0) {}
|
|
|
|
ZT_ALWAYS_INLINE MulticastGroup(const MAC &m,uint32_t a) :
|
|
_mac(m),
|
|
_adi(a) {}
|
|
|
|
/**
|
|
* Derive the multicast group used for address resolution (ARP/NDP) for an IP
|
|
*
|
|
* @param ip IP address (port field is ignored)
|
|
* @return Multicast group for ARP/NDP
|
|
*/
|
|
static ZT_ALWAYS_INLINE MulticastGroup deriveMulticastGroupForAddressResolution(const InetAddress &ip)
|
|
{
|
|
if (ip.isV4()) {
|
|
// IPv4 wants broadcast MACs, so we shove the V4 address itself into
|
|
// the Multicast Group ADI field. Making V4 ARP work is basically why
|
|
// ADI was added, as well as handling other things that want mindless
|
|
// Ethernet broadcast to all.
|
|
return MulticastGroup(MAC(0xffffffffffffULL),Utils::ntoh(*((const uint32_t *)ip.rawIpData())));
|
|
} else if (ip.isV6()) {
|
|
// IPv6 is better designed in this respect. We can compute the IPv6
|
|
// multicast address directly from the IP address, and it gives us
|
|
// 24 bits of uniqueness. Collisions aren't likely to be common enough
|
|
// to care about.
|
|
const unsigned char *a = (const unsigned char *)ip.rawIpData();
|
|
return MulticastGroup(MAC(0x33,0x33,0xff,a[13],a[14],a[15]),0);
|
|
}
|
|
return MulticastGroup();
|
|
}
|
|
|
|
ZT_ALWAYS_INLINE const MAC &mac() const { return _mac; }
|
|
ZT_ALWAYS_INLINE uint32_t adi() const { return _adi; }
|
|
|
|
ZT_ALWAYS_INLINE unsigned long hashCode() const { return (_mac.hashCode() + (unsigned long)_adi); }
|
|
|
|
ZT_ALWAYS_INLINE bool operator==(const MulticastGroup &g) const { return ((_mac == g._mac)&&(_adi == g._adi)); }
|
|
ZT_ALWAYS_INLINE bool operator!=(const MulticastGroup &g) const { return ((_mac != g._mac)||(_adi != g._adi)); }
|
|
ZT_ALWAYS_INLINE bool operator<(const MulticastGroup &g) const
|
|
{
|
|
if (_mac < g._mac)
|
|
return true;
|
|
else if (_mac == g._mac)
|
|
return (_adi < g._adi);
|
|
return false;
|
|
}
|
|
ZT_ALWAYS_INLINE bool operator>(const MulticastGroup &g) const { return (g < *this); }
|
|
ZT_ALWAYS_INLINE bool operator<=(const MulticastGroup &g) const { return !(g < *this); }
|
|
ZT_ALWAYS_INLINE bool operator>=(const MulticastGroup &g) const { return !(*this < g); }
|
|
|
|
/**
|
|
* Compute a 32-bit fnv1a hash of a multicast group and a network ID
|
|
*
|
|
* @param mg Multicast group
|
|
* @param nwid Network ID
|
|
* @return 32-bit relatively-unique ID
|
|
*/
|
|
static ZT_ALWAYS_INLINE uint32_t id(const MulticastGroup &mg,const uint64_t nwid)
|
|
{
|
|
const uint32_t fnv1aPrime = 0x01000193;
|
|
uint32_t i = 0x811c9dc5;
|
|
i = (((uint32_t)(nwid >> 56) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)(nwid >> 48) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)(nwid >> 40) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)(nwid >> 32) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)(nwid >> 24) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)(nwid >> 16) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)(nwid >> 8) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)nwid & 0xff) ^ i) * fnv1aPrime;
|
|
const uint64_t mac = mg._mac.toInt();
|
|
i = (((uint32_t)(mac >> 56) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)(mac >> 48) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)(mac >> 40) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)(mac >> 32) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)(mac >> 24) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)(mac >> 16) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)(mac >> 8) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((uint32_t)mac & 0xff) ^ i) * fnv1aPrime;
|
|
const uint32_t adi = mg._adi;
|
|
i = (((adi >> 24) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((adi >> 16) & 0xff) ^ i) * fnv1aPrime;
|
|
i = (((adi >> 8) & 0xff) ^ i) * fnv1aPrime;
|
|
i = ((adi & 0xff) ^ i) * fnv1aPrime;
|
|
return i;
|
|
}
|
|
|
|
private:
|
|
MAC _mac;
|
|
uint32_t _adi;
|
|
};
|
|
|
|
} // namespace ZeroTier
|
|
|
|
#endif
|