This commit is contained in:
Adam Ierymenko 2019-08-23 12:40:08 -07:00
parent b727e2a67a
commit f12370c348
No known key found for this signature in database
GPG key ID: 1657198823E52A61
4 changed files with 82 additions and 82 deletions

View file

@ -31,40 +31,40 @@ namespace ZeroTier {
class MAC
{
public:
inline MAC() : _m(0ULL) {}
inline MAC(const MAC &m) : _m(m._m) {}
ZT_ALWAYS_INLINE MAC() : _m(0ULL) {}
ZT_ALWAYS_INLINE MAC(const MAC &m) : _m(m._m) {}
inline MAC(const unsigned char a,const unsigned char b,const unsigned char c,const unsigned char d,const unsigned char e,const unsigned char f) :
ZT_ALWAYS_INLINE MAC(const unsigned char a,const unsigned char b,const unsigned char c,const unsigned char d,const unsigned char e,const unsigned char f) :
_m( ((((uint64_t)a) & 0xffULL) << 40) |
((((uint64_t)b) & 0xffULL) << 32) |
((((uint64_t)c) & 0xffULL) << 24) |
((((uint64_t)d) & 0xffULL) << 16) |
((((uint64_t)e) & 0xffULL) << 8) |
(((uint64_t)f) & 0xffULL) ) {}
inline MAC(const void *bits,unsigned int len) { setTo(bits,len); }
inline MAC(const Address &ztaddr,uint64_t nwid) { fromAddress(ztaddr,nwid); }
inline MAC(const uint64_t m) : _m(m & 0xffffffffffffULL) {}
ZT_ALWAYS_INLINE MAC(const void *bits,unsigned int len) { setTo(bits,len); }
ZT_ALWAYS_INLINE MAC(const Address &ztaddr,uint64_t nwid) { fromAddress(ztaddr,nwid); }
ZT_ALWAYS_INLINE MAC(const uint64_t m) : _m(m & 0xffffffffffffULL) {}
/**
* @return MAC in 64-bit integer
*/
inline uint64_t toInt() const { return _m; }
ZT_ALWAYS_INLINE uint64_t toInt() const { return _m; }
/**
* Set MAC to zero
*/
inline void zero() { _m = 0ULL; }
ZT_ALWAYS_INLINE void zero() { _m = 0ULL; }
/**
* @return True if MAC is non-zero
*/
inline operator bool() const { return (_m != 0ULL); }
ZT_ALWAYS_INLINE operator bool() const { return (_m != 0ULL); }
/**
* @param bits Raw MAC in big-endian byte order
* @param len Length, must be >= 6 or result is zero
*/
inline void setTo(const void *bits,unsigned int len)
ZT_ALWAYS_INLINE void setTo(const void *bits,unsigned int len)
{
if (len < 6) {
_m = 0ULL;
@ -83,7 +83,7 @@ public:
* @param buf Destination buffer for MAC in big-endian byte order
* @param len Length of buffer, must be >= 6 or nothing is copied
*/
inline void copyTo(void *buf,unsigned int len) const
ZT_ALWAYS_INLINE void copyTo(void *buf,unsigned int len) const
{
if (len < 6)
return;
@ -102,7 +102,7 @@ public:
* @param b Buffer to append to
*/
template<unsigned int C>
inline void appendTo(Buffer<C> &b) const
ZT_ALWAYS_INLINE void appendTo(Buffer<C> &b) const
{
unsigned char *p = (unsigned char *)b.appendField(6);
*(p++) = (unsigned char)((_m >> 40) & 0xff);
@ -116,17 +116,17 @@ public:
/**
* @return True if this is broadcast (all 0xff)
*/
inline bool isBroadcast() const { return (_m == 0xffffffffffffULL); }
ZT_ALWAYS_INLINE bool isBroadcast() const { return (_m == 0xffffffffffffULL); }
/**
* @return True if this is a multicast MAC
*/
inline bool isMulticast() const { return ((_m & 0x010000000000ULL) != 0ULL); }
ZT_ALWAYS_INLINE bool isMulticast() const { return ((_m & 0x010000000000ULL) != 0ULL); }
/**
* @param True if this is a locally-administered MAC
*/
inline bool isLocallyAdministered() const { return ((_m & 0x020000000000ULL) != 0ULL); }
ZT_ALWAYS_INLINE bool isLocallyAdministered() const { return ((_m & 0x020000000000ULL) != 0ULL); }
/**
* Set this MAC to a MAC derived from an address and a network ID
@ -134,7 +134,7 @@ public:
* @param ztaddr ZeroTier address
* @param nwid 64-bit network ID
*/
inline void fromAddress(const Address &ztaddr,uint64_t nwid)
ZT_ALWAYS_INLINE void fromAddress(const Address &ztaddr,uint64_t nwid)
{
uint64_t m = ((uint64_t)firstOctetForNetwork(nwid)) << 40;
m |= ztaddr.toInt(); // a is 40 bits
@ -153,7 +153,7 @@ public:
*
* @param nwid Network ID
*/
inline Address toAddress(uint64_t nwid) const
ZT_ALWAYS_INLINE Address toAddress(uint64_t nwid) const
{
uint64_t a = _m & 0xffffffffffULL; // least significant 40 bits of MAC are formed from address
a ^= ((nwid >> 8) & 0xff) << 32; // ... XORed with bits 8-48 of the nwid in little-endian byte order, so unmask it
@ -168,7 +168,7 @@ public:
* @param nwid Network ID
* @return First octet of MAC for this network
*/
static inline unsigned char firstOctetForNetwork(uint64_t nwid)
static ZT_ALWAYS_INLINE unsigned char firstOctetForNetwork(uint64_t nwid)
{
unsigned char a = ((unsigned char)(nwid & 0xfe) | 0x02); // locally administered, not multicast, from LSB of network ID
return ((a == 0x52) ? 0x32 : a); // blacklist 0x52 since it's used by KVM, libvirt, and other popular virtualization engines... seems de-facto standard on Linux
@ -178,16 +178,16 @@ public:
* @param i Value from 0 to 5 (inclusive)
* @return Byte at said position (address interpreted in big-endian order)
*/
inline uint8_t operator[](unsigned int i) const { return (uint8_t)(_m >> (40 - (i * 8))); }
ZT_ALWAYS_INLINE uint8_t operator[](unsigned int i) const { return (uint8_t)(_m >> (40 - (i * 8))); }
/**
* @return 6, which is the number of bytes in a MAC, for container compliance
*/
inline unsigned int size() const { return 6; }
ZT_ALWAYS_INLINE unsigned int size() const { return 6; }
inline unsigned long hashCode() const { return (unsigned long)_m; }
ZT_ALWAYS_INLINE unsigned long hashCode() const { return (unsigned long)_m; }
inline char *toString(char buf[18]) const
ZT_ALWAYS_INLINE char *toString(char buf[18]) const
{
buf[0] = Utils::HEXCHARS[(_m >> 44) & 0xf];
buf[1] = Utils::HEXCHARS[(_m >> 40) & 0xf];
@ -210,23 +210,23 @@ public:
return buf;
}
inline MAC &operator=(const MAC &m)
ZT_ALWAYS_INLINE MAC &operator=(const MAC &m)
{
_m = m._m;
return *this;
}
inline MAC &operator=(const uint64_t m)
ZT_ALWAYS_INLINE MAC &operator=(const uint64_t m)
{
_m = m & 0xffffffffffffULL;
return *this;
}
inline bool operator==(const MAC &m) const { return (_m == m._m); }
inline bool operator!=(const MAC &m) const { return (_m != m._m); }
inline bool operator<(const MAC &m) const { return (_m < m._m); }
inline bool operator<=(const MAC &m) const { return (_m <= m._m); }
inline bool operator>(const MAC &m) const { return (_m > m._m); }
inline bool operator>=(const MAC &m) const { return (_m >= m._m); }
ZT_ALWAYS_INLINE bool operator==(const MAC &m) const { return (_m == m._m); }
ZT_ALWAYS_INLINE bool operator!=(const MAC &m) const { return (_m != m._m); }
ZT_ALWAYS_INLINE bool operator<(const MAC &m) const { return (_m < m._m); }
ZT_ALWAYS_INLINE bool operator<=(const MAC &m) const { return (_m <= m._m); }
ZT_ALWAYS_INLINE bool operator>(const MAC &m) const { return (_m > m._m); }
ZT_ALWAYS_INLINE bool operator>=(const MAC &m) const { return (_m >= m._m); }
private:
uint64_t _m;