2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2019-01-14 18:25:53 +00:00
|
|
|
* Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
|
2013-07-04 20:56:19 +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
|
2019-01-14 18:25:53 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-04-28 03:47:25 +00:00
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* You can be released from the requirements of the license by purchasing
|
|
|
|
* a commercial license. Buying such a license is mandatory as soon as you
|
|
|
|
* develop commercial closed-source software that incorporates or links
|
|
|
|
* directly against ZeroTier software without disclosing the source code
|
|
|
|
* of your own application.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
|
2013-12-07 00:49:20 +00:00
|
|
|
#ifndef ZT_ADDRESS_HPP
|
|
|
|
#define ZT_ADDRESS_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-25 17:24:39 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <stdint.h>
|
2013-07-25 17:24:39 +00:00
|
|
|
#include <string.h>
|
2013-08-30 21:05:43 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <string>
|
2013-08-30 21:05:43 +00:00
|
|
|
|
2013-10-05 18:15:59 +00:00
|
|
|
#include "Constants.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Utils.hpp"
|
2013-07-25 17:24:39 +00:00
|
|
|
#include "Buffer.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
2013-07-25 17:24:39 +00:00
|
|
|
* A ZeroTier address
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
class Address
|
|
|
|
{
|
|
|
|
public:
|
2017-02-04 08:23:31 +00:00
|
|
|
Address() : _a(0) {}
|
|
|
|
Address(const Address &a) : _a(a._a) {}
|
|
|
|
Address(uint64_t a) : _a(a & 0xffffffffffULL) {}
|
2013-07-29 17:56:20 +00:00
|
|
|
|
2013-07-25 17:24:39 +00:00
|
|
|
/**
|
|
|
|
* @param bits Raw address -- 5 bytes, big-endian byte order
|
2013-07-29 17:56:20 +00:00
|
|
|
* @param len Length of array
|
2013-07-25 17:24:39 +00:00
|
|
|
*/
|
2013-07-29 17:56:20 +00:00
|
|
|
Address(const void *bits,unsigned int len)
|
2013-07-25 17:24:39 +00:00
|
|
|
{
|
2013-07-29 17:56:20 +00:00
|
|
|
setTo(bits,len);
|
2013-07-25 17:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline Address &operator=(const Address &a)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2013-07-25 17:24:39 +00:00
|
|
|
_a = a._a;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Address &operator=(const uint64_t a)
|
|
|
|
{
|
|
|
|
_a = (a & 0xffffffffffULL);
|
|
|
|
return *this;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-25 17:24:39 +00:00
|
|
|
* @param bits Raw address -- 5 bytes, big-endian byte order
|
2013-07-29 17:56:20 +00:00
|
|
|
* @param len Length of array
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2013-07-29 17:56:20 +00:00
|
|
|
inline void setTo(const void *bits,unsigned int len)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2013-07-29 17:56:20 +00:00
|
|
|
if (len < ZT_ADDRESS_LENGTH) {
|
|
|
|
_a = 0;
|
|
|
|
return;
|
|
|
|
}
|
2013-07-25 17:24:39 +00:00
|
|
|
const unsigned char *b = (const unsigned char *)bits;
|
|
|
|
uint64_t a = ((uint64_t)*b++) << 32;
|
|
|
|
a |= ((uint64_t)*b++) << 24;
|
|
|
|
a |= ((uint64_t)*b++) << 16;
|
|
|
|
a |= ((uint64_t)*b++) << 8;
|
|
|
|
a |= ((uint64_t)*b);
|
|
|
|
_a = a;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-25 17:24:39 +00:00
|
|
|
* @param bits Buffer to hold 5-byte address in big-endian byte order
|
2013-07-29 17:56:20 +00:00
|
|
|
* @param len Length of array
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2013-07-29 17:56:20 +00:00
|
|
|
inline void copyTo(void *bits,unsigned int len) const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2013-07-29 17:56:20 +00:00
|
|
|
if (len < ZT_ADDRESS_LENGTH)
|
|
|
|
return;
|
2013-07-25 17:24:39 +00:00
|
|
|
unsigned char *b = (unsigned char *)bits;
|
|
|
|
*(b++) = (unsigned char)((_a >> 32) & 0xff);
|
|
|
|
*(b++) = (unsigned char)((_a >> 24) & 0xff);
|
|
|
|
*(b++) = (unsigned char)((_a >> 16) & 0xff);
|
|
|
|
*(b++) = (unsigned char)((_a >> 8) & 0xff);
|
|
|
|
*b = (unsigned char)(_a & 0xff);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2013-07-25 17:24:39 +00:00
|
|
|
/**
|
|
|
|
* Append to a buffer in big-endian byte order
|
|
|
|
*
|
|
|
|
* @param b Buffer to append to
|
|
|
|
*/
|
|
|
|
template<unsigned int C>
|
|
|
|
inline void appendTo(Buffer<C> &b) const
|
|
|
|
{
|
2013-10-18 18:16:53 +00:00
|
|
|
unsigned char *p = (unsigned char *)b.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);
|
2013-07-25 17:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Integer containing address (0 to 2^40)
|
|
|
|
*/
|
|
|
|
inline uint64_t toInt() const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2013-07-25 17:24:39 +00:00
|
|
|
return _a;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 19:14:21 +00:00
|
|
|
/**
|
|
|
|
* @return Hash code for use with Hashtable
|
|
|
|
*/
|
|
|
|
inline unsigned long hashCode() const
|
|
|
|
{
|
|
|
|
return (unsigned long)_a;
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* @return Hexadecimal string
|
|
|
|
*/
|
2017-07-06 23:11:11 +00:00
|
|
|
inline char *toString(char buf[11]) const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2017-07-06 23:11:11 +00:00
|
|
|
return Utils::hex10(_a,buf);
|
2014-09-12 23:57:37 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* @return True if this address is not zero
|
|
|
|
*/
|
2017-02-04 08:23:31 +00:00
|
|
|
inline operator bool() const { return (_a != 0); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-10-04 16:24:21 +00:00
|
|
|
/**
|
|
|
|
* Set to null/zero
|
|
|
|
*/
|
2017-02-04 08:23:31 +00:00
|
|
|
inline void zero() { _a = 0; }
|
2013-10-04 16:24:21 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Check if this address is reserved
|
2015-09-04 19:14:21 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* The all-zero null address and any address beginning with 0xff are
|
|
|
|
* reserved. (0xff is reserved for future use to designate possibly
|
|
|
|
* longer addresses, addresses based on IPv6 innards, etc.)
|
2015-09-04 19:14:21 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @return True if address is reserved and may not be used
|
|
|
|
*/
|
|
|
|
inline bool isReserved() const
|
|
|
|
{
|
2013-07-25 17:24:39 +00:00
|
|
|
return ((!_a)||((_a >> 32) == ZT_ADDRESS_RESERVED_PREFIX));
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2013-07-25 17:24:39 +00:00
|
|
|
/**
|
|
|
|
* @param i Value from 0 to 4 (inclusive)
|
|
|
|
* @return Byte at said position (address interpreted in big-endian order)
|
|
|
|
*/
|
2017-02-04 08:23:31 +00:00
|
|
|
inline unsigned char operator[](unsigned int i) const { return (unsigned char)((_a >> (32 - (i * 8))) & 0xff); }
|
|
|
|
|
|
|
|
inline bool operator==(const uint64_t &a) const { return (_a == (a & 0xffffffffffULL)); }
|
|
|
|
inline bool operator!=(const uint64_t &a) const { return (_a != (a & 0xffffffffffULL)); }
|
|
|
|
inline bool operator>(const uint64_t &a) const { return (_a > (a & 0xffffffffffULL)); }
|
|
|
|
inline bool operator<(const uint64_t &a) const { return (_a < (a & 0xffffffffffULL)); }
|
|
|
|
inline bool operator>=(const uint64_t &a) const { return (_a >= (a & 0xffffffffffULL)); }
|
|
|
|
inline bool operator<=(const uint64_t &a) const { return (_a <= (a & 0xffffffffffULL)); }
|
|
|
|
|
|
|
|
inline bool operator==(const Address &a) const { return (_a == a._a); }
|
|
|
|
inline bool operator!=(const Address &a) const { return (_a != a._a); }
|
|
|
|
inline bool operator>(const Address &a) const { return (_a > a._a); }
|
|
|
|
inline bool operator<(const Address &a) const { return (_a < a._a); }
|
|
|
|
inline bool operator>=(const Address &a) const { return (_a >= a._a); }
|
|
|
|
inline bool operator<=(const Address &a) const { return (_a <= a._a); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-25 17:24:39 +00:00
|
|
|
private:
|
|
|
|
uint64_t _a;
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|