2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2017-04-28 03:47:25 +00:00
|
|
|
* Copyright (C) 2011-2017 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
|
|
|
|
* 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_INETADDRESS_HPP
|
|
|
|
#define ZT_INETADDRESS_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-08-13 01:25:36 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <string>
|
|
|
|
|
2013-08-13 01:25:36 +00:00
|
|
|
#include "Constants.hpp"
|
2015-04-01 00:53:34 +00:00
|
|
|
#include "../include/ZeroTierOne.h"
|
2014-01-23 22:15:00 +00:00
|
|
|
#include "Utils.hpp"
|
2014-01-31 23:55:45 +00:00
|
|
|
#include "MAC.hpp"
|
2015-10-01 22:40:54 +00:00
|
|
|
#include "Buffer.hpp"
|
2013-08-13 01:25:36 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
2015-10-28 16:11:30 +00:00
|
|
|
/**
|
|
|
|
* Maximum integer value of enum IpScope
|
|
|
|
*/
|
|
|
|
#define ZT_INETADDRESS_MAX_SCOPE 7
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
2015-04-01 00:53:34 +00:00
|
|
|
* Extends sockaddr_storage with friendly C++ methods
|
2014-03-31 18:41:14 +00:00
|
|
|
*
|
2015-04-07 17:57:59 +00:00
|
|
|
* This is basically a "mixin" for sockaddr_storage. It adds methods and
|
|
|
|
* operators, but does not modify the structure. This can be cast to/from
|
2015-10-28 16:38:33 +00:00
|
|
|
* sockaddr_storage and used interchangeably. DO NOT change this by e.g.
|
|
|
|
* adding non-static fields, since much code depends on this identity.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
struct InetAddress : public sockaddr_storage
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Loopback IPv4 address (no port)
|
|
|
|
*/
|
|
|
|
static const InetAddress LO4;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loopback IPV6 address (no port)
|
|
|
|
*/
|
|
|
|
static const InetAddress LO6;
|
|
|
|
|
2015-04-07 17:57:59 +00:00
|
|
|
/**
|
|
|
|
* IP address scope
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
|
|
|
* Note that these values are in ascending order of path preference and
|
2015-10-28 16:11:30 +00:00
|
|
|
* MUST remain that way or Path must be changed to reflect. Also be sure
|
|
|
|
* to change ZT_INETADDRESS_MAX_SCOPE if the max changes.
|
2015-04-07 17:57:59 +00:00
|
|
|
*/
|
|
|
|
enum IpScope
|
|
|
|
{
|
2015-07-13 17:03:04 +00:00
|
|
|
IP_SCOPE_NONE = 0, // NULL or not an IP address
|
|
|
|
IP_SCOPE_MULTICAST = 1, // 224.0.0.0 and other V4/V6 multicast IPs
|
|
|
|
IP_SCOPE_LOOPBACK = 2, // 127.0.0.1, ::1, etc.
|
|
|
|
IP_SCOPE_PSEUDOPRIVATE = 3, // 28.x.x.x, etc. -- unofficially unrouted IPv4 blocks often "bogarted"
|
|
|
|
IP_SCOPE_GLOBAL = 4, // globally routable IP address (all others)
|
|
|
|
IP_SCOPE_LINK_LOCAL = 5, // 169.254.x.x, IPv6 LL
|
|
|
|
IP_SCOPE_SHARED = 6, // 100.64.0.0/10, shared space for e.g. carrier-grade NAT
|
|
|
|
IP_SCOPE_PRIVATE = 7 // 10.x.x.x, 192.168.x.x, etc.
|
2015-04-07 17:57:59 +00:00
|
|
|
};
|
|
|
|
|
2015-04-01 00:53:34 +00:00
|
|
|
InetAddress() throw() { memset(this,0,sizeof(InetAddress)); }
|
|
|
|
InetAddress(const InetAddress &a) throw() { memcpy(this,&a,sizeof(InetAddress)); }
|
2015-04-14 21:49:34 +00:00
|
|
|
InetAddress(const InetAddress *a) throw() { memcpy(this,a,sizeof(InetAddress)); }
|
2015-04-01 00:53:34 +00:00
|
|
|
InetAddress(const struct sockaddr_storage &ss) throw() { *this = ss; }
|
|
|
|
InetAddress(const struct sockaddr_storage *ss) throw() { *this = ss; }
|
|
|
|
InetAddress(const struct sockaddr &sa) throw() { *this = sa; }
|
|
|
|
InetAddress(const struct sockaddr *sa) throw() { *this = sa; }
|
|
|
|
InetAddress(const struct sockaddr_in &sa) throw() { *this = sa; }
|
|
|
|
InetAddress(const struct sockaddr_in *sa) throw() { *this = sa; }
|
|
|
|
InetAddress(const struct sockaddr_in6 &sa) throw() { *this = sa; }
|
|
|
|
InetAddress(const struct sockaddr_in6 *sa) throw() { *this = sa; }
|
2014-07-02 22:59:08 +00:00
|
|
|
InetAddress(const void *ipBytes,unsigned int ipLen,unsigned int port) throw() { this->set(ipBytes,ipLen,port); }
|
|
|
|
InetAddress(const uint32_t ipv4,unsigned int port) throw() { this->set(&ipv4,4,port); }
|
|
|
|
InetAddress(const std::string &ip,unsigned int port) throw() { this->set(ip,port); }
|
|
|
|
InetAddress(const std::string &ipSlashPort) throw() { this->fromString(ipSlashPort); }
|
|
|
|
InetAddress(const char *ipSlashPort) throw() { this->fromString(std::string(ipSlashPort)); }
|
2013-10-01 20:01:36 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
inline InetAddress &operator=(const InetAddress &a)
|
|
|
|
throw()
|
|
|
|
{
|
2015-10-09 21:51:38 +00:00
|
|
|
if (&a != this)
|
|
|
|
memcpy(this,&a,sizeof(InetAddress));
|
2013-07-04 20:56:19 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-04-14 21:49:34 +00:00
|
|
|
inline InetAddress &operator=(const InetAddress *a)
|
|
|
|
throw()
|
|
|
|
{
|
2015-10-09 21:51:38 +00:00
|
|
|
if (a != this)
|
|
|
|
memcpy(this,a,sizeof(InetAddress));
|
2015-04-14 21:49:34 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-04-01 00:53:34 +00:00
|
|
|
inline InetAddress &operator=(const struct sockaddr_storage &ss)
|
|
|
|
throw()
|
|
|
|
{
|
2015-10-09 21:51:38 +00:00
|
|
|
if (reinterpret_cast<const InetAddress *>(&ss) != this)
|
|
|
|
memcpy(this,&ss,sizeof(InetAddress));
|
2015-04-01 00:53:34 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr_storage *ss)
|
|
|
|
throw()
|
|
|
|
{
|
2015-10-09 21:51:38 +00:00
|
|
|
if (reinterpret_cast<const InetAddress *>(ss) != this)
|
|
|
|
memcpy(this,ss,sizeof(InetAddress));
|
2015-04-01 00:53:34 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr_in &sa)
|
|
|
|
throw()
|
|
|
|
{
|
2015-10-09 21:51:38 +00:00
|
|
|
if (reinterpret_cast<const InetAddress *>(&sa) != this) {
|
|
|
|
memset(this,0,sizeof(InetAddress));
|
|
|
|
memcpy(this,&sa,sizeof(struct sockaddr_in));
|
|
|
|
}
|
2015-04-01 00:53:34 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr_in *sa)
|
|
|
|
throw()
|
|
|
|
{
|
2015-10-09 21:51:38 +00:00
|
|
|
if (reinterpret_cast<const InetAddress *>(sa) != this) {
|
|
|
|
memset(this,0,sizeof(InetAddress));
|
|
|
|
memcpy(this,sa,sizeof(struct sockaddr_in));
|
|
|
|
}
|
2015-04-01 00:53:34 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr_in6 &sa)
|
|
|
|
throw()
|
|
|
|
{
|
2015-10-09 21:51:38 +00:00
|
|
|
if (reinterpret_cast<const InetAddress *>(&sa) != this) {
|
|
|
|
memset(this,0,sizeof(InetAddress));
|
|
|
|
memcpy(this,&sa,sizeof(struct sockaddr_in6));
|
|
|
|
}
|
2015-04-01 00:53:34 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr_in6 *sa)
|
|
|
|
throw()
|
|
|
|
{
|
2015-10-09 21:51:38 +00:00
|
|
|
if (reinterpret_cast<const InetAddress *>(sa) != this) {
|
|
|
|
memset(this,0,sizeof(InetAddress));
|
|
|
|
memcpy(this,sa,sizeof(struct sockaddr_in6));
|
|
|
|
}
|
2015-04-01 00:53:34 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr &sa)
|
2013-07-04 20:56:19 +00:00
|
|
|
throw()
|
|
|
|
{
|
2015-10-09 21:51:38 +00:00
|
|
|
if (reinterpret_cast<const InetAddress *>(&sa) != this) {
|
|
|
|
memset(this,0,sizeof(InetAddress));
|
|
|
|
switch(sa.sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
memcpy(this,&sa,sizeof(struct sockaddr_in));
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
memcpy(this,&sa,sizeof(struct sockaddr_in6));
|
|
|
|
break;
|
|
|
|
}
|
2015-04-01 00:53:34 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr *sa)
|
|
|
|
throw()
|
|
|
|
{
|
2015-10-09 21:51:38 +00:00
|
|
|
if (reinterpret_cast<const InetAddress *>(sa) != this) {
|
|
|
|
memset(this,0,sizeof(InetAddress));
|
|
|
|
switch(sa->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
memcpy(this,sa,sizeof(struct sockaddr_in));
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
memcpy(this,sa,sizeof(struct sockaddr_in6));
|
|
|
|
break;
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2015-04-01 00:53:34 +00:00
|
|
|
return *this;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 17:57:59 +00:00
|
|
|
/**
|
|
|
|
* @return IP scope classification (e.g. loopback, link-local, private, global)
|
|
|
|
*/
|
|
|
|
IpScope ipScope() const
|
|
|
|
throw();
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Set from a string-format IP and a port
|
|
|
|
*
|
|
|
|
* @param ip IP address in V4 or V6 ASCII notation
|
|
|
|
* @param port Port or 0 for none
|
|
|
|
*/
|
|
|
|
void set(const std::string &ip,unsigned int port)
|
|
|
|
throw();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set from a raw IP and port number
|
|
|
|
*
|
|
|
|
* @param ipBytes Bytes of IP address in network byte order
|
|
|
|
* @param ipLen Length of IP address: 4 or 16
|
|
|
|
* @param port Port number or 0 for none
|
|
|
|
*/
|
2014-07-02 22:59:08 +00:00
|
|
|
void set(const void *ipBytes,unsigned int ipLen,unsigned int port)
|
|
|
|
throw();
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the port component
|
|
|
|
*
|
|
|
|
* @param port Port, 0 to 65535
|
|
|
|
*/
|
|
|
|
inline void setPort(unsigned int port)
|
|
|
|
{
|
2015-04-01 00:53:34 +00:00
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
reinterpret_cast<struct sockaddr_in *>(this)->sin_port = Utils::hton((uint16_t)port);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_port = Utils::hton((uint16_t)port);
|
|
|
|
break;
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 22:02:40 +00:00
|
|
|
/**
|
|
|
|
* @return True if this network/netmask route describes a default route (e.g. 0.0.0.0/0)
|
|
|
|
*/
|
2016-06-15 22:46:57 +00:00
|
|
|
inline bool isDefaultRoute() const
|
2016-06-15 22:02:40 +00:00
|
|
|
{
|
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET:
|
2016-06-15 22:46:57 +00:00
|
|
|
return ( (reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr == 0) && (reinterpret_cast<const struct sockaddr_in *>(this)->sin_port == 0) );
|
2016-06-15 22:02:40 +00:00
|
|
|
case AF_INET6:
|
2016-06-15 22:46:57 +00:00
|
|
|
const uint8_t *ipb = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
|
2016-06-15 22:02:40 +00:00
|
|
|
for(int i=0;i<16;++i) {
|
|
|
|
if (ipb[i])
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-15 22:46:57 +00:00
|
|
|
return (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port == 0);
|
2016-06-15 22:02:40 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* @return ASCII IP/port format representation
|
|
|
|
*/
|
|
|
|
std::string toString() const;
|
|
|
|
|
|
|
|
/**
|
2015-04-01 00:53:34 +00:00
|
|
|
* @return IP portion only, in ASCII string format
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
std::string toIpString() const;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2015-04-01 00:53:34 +00:00
|
|
|
* @param ipSlashPort ASCII IP/port format notation
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
void fromString(const std::string &ipSlashPort);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Port or 0 if no port component defined
|
|
|
|
*/
|
|
|
|
inline unsigned int port() const
|
|
|
|
throw()
|
|
|
|
{
|
2015-04-01 00:53:34 +00:00
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET: return Utils::ntoh((uint16_t)(reinterpret_cast<const struct sockaddr_in *>(this)->sin_port));
|
|
|
|
case AF_INET6: return Utils::ntoh((uint16_t)(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port));
|
2014-07-02 22:59:08 +00:00
|
|
|
default: return 0;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for port()
|
|
|
|
*
|
|
|
|
* This just aliases port() to make code more readable when netmask bits
|
|
|
|
* are stuffed there, as they are in Network, EthernetTap, and a few other
|
|
|
|
* spots.
|
|
|
|
*
|
|
|
|
* @return Netmask bits
|
|
|
|
*/
|
2014-07-02 22:59:08 +00:00
|
|
|
inline unsigned int netmaskBits() const throw() { return port(); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2016-08-16 01:49:50 +00:00
|
|
|
/**
|
|
|
|
* @return True if netmask bits is valid for the address type
|
|
|
|
*/
|
|
|
|
inline bool netmaskBitsValid() const
|
|
|
|
{
|
|
|
|
const unsigned int n = port();
|
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET: return (n <= 32);
|
|
|
|
case AF_INET6: return (n <= 128);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-13 09:34:31 +00:00
|
|
|
/**
|
|
|
|
* Alias for port()
|
|
|
|
*
|
|
|
|
* This just aliases port() because for gateways we use this field to
|
|
|
|
* store the gateway metric.
|
|
|
|
*
|
|
|
|
* @return Gateway metric
|
|
|
|
*/
|
|
|
|
inline unsigned int metric() const throw() { return port(); }
|
|
|
|
|
2014-01-23 22:15:00 +00:00
|
|
|
/**
|
|
|
|
* Construct a full netmask as an InetAddress
|
2015-11-12 18:47:50 +00:00
|
|
|
*
|
|
|
|
* @return Netmask such as 255.255.255.0 if this address is /24 (port field will be unchanged)
|
2014-01-23 22:15:00 +00:00
|
|
|
*/
|
2015-11-12 18:47:50 +00:00
|
|
|
InetAddress netmask() const;
|
2014-01-23 22:15:00 +00:00
|
|
|
|
2014-09-04 17:36:25 +00:00
|
|
|
/**
|
|
|
|
* Constructs a broadcast address from a network/netmask address
|
|
|
|
*
|
2015-11-12 18:47:50 +00:00
|
|
|
* This is only valid for IPv4 and will return a NULL InetAddress for other
|
|
|
|
* address families.
|
|
|
|
*
|
2014-09-04 17:36:25 +00:00
|
|
|
* @return Broadcast address (only IP portion is meaningful)
|
|
|
|
*/
|
2015-11-12 18:47:50 +00:00
|
|
|
InetAddress broadcast() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the network -- a.k.a. the IP ANDed with the netmask
|
|
|
|
*
|
|
|
|
* @return Network e.g. 10.0.1.0/24 from 10.0.1.200/24
|
|
|
|
*/
|
|
|
|
InetAddress network() const;
|
2014-09-04 17:36:25 +00:00
|
|
|
|
2017-05-04 22:25:48 +00:00
|
|
|
#ifdef ZT_SDK
|
|
|
|
/**
|
|
|
|
* Test whether this IPv6 prefix matches the prefix of a given IPv6 address
|
|
|
|
*
|
|
|
|
* @param addr Address to check
|
|
|
|
* @return True if this IPv6 prefix matches the prefix of a given IPv6 address
|
|
|
|
*/
|
|
|
|
bool isEqualPrefix(const InetAddress &addr) const;
|
|
|
|
#endif
|
|
|
|
|
2016-01-11 18:17:44 +00:00
|
|
|
/**
|
|
|
|
* Test whether this IP/netmask contains this address
|
|
|
|
*
|
|
|
|
* @param addr Address to check
|
|
|
|
* @return True if this IP/netmask (route) contains this address
|
|
|
|
*/
|
|
|
|
bool containsAddress(const InetAddress &addr) const;
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* @return True if this is an IPv4 address
|
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
inline bool isV4() const throw() { return (ss_family == AF_INET); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return True if this is an IPv6 address
|
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
inline bool isV6() const throw() { return (ss_family == AF_INET6); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-28 16:38:33 +00:00
|
|
|
* @return pointer to raw address bytes or NULL if not available
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
inline const void *rawIpData() const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2015-04-01 00:53:34 +00:00
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET: return (const void *)&(reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr);
|
|
|
|
case AF_INET6: return (const void *)(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
|
|
|
|
default: return 0;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 17:28:54 +00:00
|
|
|
/**
|
|
|
|
* @return InetAddress containing only the IP portion of this address and a zero port, or NULL if not IPv4 or IPv6
|
|
|
|
*/
|
|
|
|
inline InetAddress ipOnly() const
|
|
|
|
{
|
|
|
|
InetAddress r;
|
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
r.ss_family = AF_INET;
|
|
|
|
reinterpret_cast<struct sockaddr_in *>(&r)->sin_addr.s_addr = reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
r.ss_family = AF_INET6;
|
|
|
|
memcpy(reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,16);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-04-10 03:54:00 +00:00
|
|
|
/**
|
2015-10-28 00:59:17 +00:00
|
|
|
* Performs an IP-only comparison or, if that is impossible, a memcmp()
|
|
|
|
*
|
2015-04-10 03:54:00 +00:00
|
|
|
* @param a InetAddress to compare again
|
|
|
|
* @return True if only IP portions are equal (false for non-IP or null addresses)
|
|
|
|
*/
|
|
|
|
inline bool ipsEqual(const InetAddress &a) const
|
|
|
|
{
|
2015-10-28 00:59:17 +00:00
|
|
|
if (ss_family == a.ss_family) {
|
|
|
|
if (ss_family == AF_INET)
|
|
|
|
return (reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr == reinterpret_cast<const struct sockaddr_in *>(&a)->sin_addr.s_addr);
|
|
|
|
if (ss_family == AF_INET6)
|
|
|
|
return (memcmp(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_addr.s6_addr,16) == 0);
|
|
|
|
return (memcmp(this,&a,sizeof(InetAddress)) == 0);
|
2015-04-10 03:54:00 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-28 20:45:25 +00:00
|
|
|
inline unsigned long hashCode() const
|
|
|
|
{
|
|
|
|
if (ss_family == AF_INET) {
|
|
|
|
return ((unsigned long)reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr + (unsigned long)reinterpret_cast<const struct sockaddr_in *>(this)->sin_port);
|
|
|
|
} else if (ss_family == AF_INET6) {
|
|
|
|
unsigned long tmp = reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port;
|
|
|
|
const uint8_t *a = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
|
|
|
|
for(long i=0;i<16;++i)
|
|
|
|
reinterpret_cast<uint8_t *>(&tmp)[i % sizeof(tmp)] ^= a[i];
|
|
|
|
return tmp;
|
|
|
|
} else {
|
|
|
|
unsigned long tmp = reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port;
|
|
|
|
const uint8_t *a = reinterpret_cast<const uint8_t *>(this);
|
2016-11-18 21:01:45 +00:00
|
|
|
for(long i=0;i<(long)sizeof(InetAddress);++i)
|
2016-09-28 20:45:25 +00:00
|
|
|
reinterpret_cast<uint8_t *>(&tmp)[i % sizeof(tmp)] ^= a[i];
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Set to null/zero
|
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
inline void zero() throw() { memset(this,0,sizeof(InetAddress)); }
|
2014-07-02 22:59:08 +00:00
|
|
|
|
2015-06-29 17:21:28 +00:00
|
|
|
/**
|
|
|
|
* Check whether this is a network/route rather than an IP assignment
|
|
|
|
*
|
|
|
|
* A network is an IP/netmask where everything after the netmask is
|
|
|
|
* zero e.g. 10.0.0.0/8.
|
|
|
|
*
|
|
|
|
* @return True if everything after netmask bits is zero
|
|
|
|
*/
|
|
|
|
bool isNetwork() const
|
|
|
|
throw();
|
|
|
|
|
2016-11-18 20:59:04 +00:00
|
|
|
/**
|
|
|
|
* @return 14-bit (0-16383) hash of this IP's first 24 or 48 bits (for V4 or V6) for rate limiting code, or 0 if non-IP
|
|
|
|
*/
|
|
|
|
inline unsigned long rateGateHash() const
|
|
|
|
{
|
|
|
|
unsigned long h = 0;
|
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
h = (Utils::ntoh((uint32_t)reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr) & 0xffffff00) >> 8;
|
|
|
|
h ^= (h >> 14);
|
|
|
|
break;
|
|
|
|
case AF_INET6: {
|
|
|
|
const uint8_t *ip = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
|
|
|
|
h = ((unsigned long)ip[0]); h <<= 1;
|
|
|
|
h += ((unsigned long)ip[1]); h <<= 1;
|
|
|
|
h += ((unsigned long)ip[2]); h <<= 1;
|
|
|
|
h += ((unsigned long)ip[3]); h <<= 1;
|
|
|
|
h += ((unsigned long)ip[4]); h <<= 1;
|
|
|
|
h += ((unsigned long)ip[5]);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
return (h & 0x3fff);
|
|
|
|
}
|
|
|
|
|
2014-07-02 22:59:08 +00:00
|
|
|
/**
|
|
|
|
* @return True if address family is non-zero
|
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
inline operator bool() const throw() { return (ss_family != 0); }
|
2014-07-02 22:59:08 +00:00
|
|
|
|
2015-10-01 22:40:54 +00:00
|
|
|
template<unsigned int C>
|
|
|
|
inline void serialize(Buffer<C> &b) const
|
|
|
|
{
|
2015-10-07 17:30:47 +00:00
|
|
|
// This is used in the protocol and must be the same as describe in places
|
|
|
|
// like VERB_HELLO in Packet.hpp.
|
2015-10-01 22:40:54 +00:00
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
b.append((uint8_t)0x04);
|
|
|
|
b.append(&(reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr),4);
|
|
|
|
b.append((uint16_t)port()); // just in case sin_port != uint16_t
|
|
|
|
return;
|
|
|
|
case AF_INET6:
|
|
|
|
b.append((uint8_t)0x06);
|
|
|
|
b.append(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,16);
|
|
|
|
b.append((uint16_t)port()); // just in case sin_port != uint16_t
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
b.append((uint8_t)0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned int C>
|
|
|
|
inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
|
|
|
|
{
|
|
|
|
memset(this,0,sizeof(InetAddress));
|
2015-10-07 17:30:47 +00:00
|
|
|
unsigned int p = startAt;
|
2015-10-01 22:40:54 +00:00
|
|
|
switch(b[p++]) {
|
|
|
|
case 0:
|
|
|
|
return 1;
|
2015-10-26 17:42:30 +00:00
|
|
|
case 0x01:
|
|
|
|
// TODO: Ethernet address (but accept for forward compatibility)
|
|
|
|
return 7;
|
|
|
|
case 0x02:
|
2015-11-03 18:46:41 +00:00
|
|
|
// TODO: Bluetooth address (but accept for forward compatibility)
|
2015-10-26 17:42:30 +00:00
|
|
|
return 7;
|
|
|
|
case 0x03:
|
|
|
|
// TODO: Other address types (but accept for forward compatibility)
|
|
|
|
// These could be extended/optional things like AF_UNIX, LTE Direct, shared memory, etc.
|
|
|
|
return (unsigned int)(b.template at<uint16_t>(p) + 3); // other addresses begin with 16-bit non-inclusive length
|
2015-10-01 22:40:54 +00:00
|
|
|
case 0x04:
|
|
|
|
ss_family = AF_INET;
|
|
|
|
memcpy(&(reinterpret_cast<struct sockaddr_in *>(this)->sin_addr.s_addr),b.field(p,4),4); p += 4;
|
|
|
|
reinterpret_cast<struct sockaddr_in *>(this)->sin_port = Utils::hton(b.template at<uint16_t>(p)); p += 2;
|
|
|
|
break;
|
|
|
|
case 0x06:
|
|
|
|
ss_family = AF_INET6;
|
|
|
|
memcpy(reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,b.field(p,16),16); p += 16;
|
|
|
|
reinterpret_cast<struct sockaddr_in *>(this)->sin_port = Utils::hton(b.template at<uint16_t>(p)); p += 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw std::invalid_argument("invalid serialized InetAddress");
|
|
|
|
}
|
|
|
|
return (p - startAt);
|
|
|
|
}
|
|
|
|
|
2015-04-14 21:49:34 +00:00
|
|
|
bool operator==(const InetAddress &a) const throw();
|
|
|
|
bool operator<(const InetAddress &a) const throw();
|
2014-07-02 22:59:08 +00:00
|
|
|
inline bool operator!=(const InetAddress &a) const throw() { return !(*this == a); }
|
|
|
|
inline bool operator>(const InetAddress &a) const throw() { return (a < *this); }
|
|
|
|
inline bool operator<=(const InetAddress &a) const throw() { return !(a < *this); }
|
|
|
|
inline bool operator>=(const InetAddress &a) const throw() { return !(*this < a); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-01-31 23:55:45 +00:00
|
|
|
/**
|
|
|
|
* @param mac MAC address seed
|
|
|
|
* @return IPv6 link-local address
|
|
|
|
*/
|
2016-06-24 13:40:50 +00:00
|
|
|
static InetAddress makeIpv6LinkLocal(const MAC &mac);
|
2015-09-18 04:30:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute private IPv6 unicast address from network ID and ZeroTier address
|
|
|
|
*
|
|
|
|
* This generates a private unicast IPv6 address that is mostly compliant
|
|
|
|
* with the letter of RFC4193 and certainly compliant in spirit.
|
|
|
|
*
|
|
|
|
* RFC4193 specifies a format of:
|
|
|
|
*
|
|
|
|
* | 7 bits |1| 40 bits | 16 bits | 64 bits |
|
|
|
|
* | Prefix |L| Global ID | Subnet ID | Interface ID |
|
|
|
|
*
|
|
|
|
* The 'L' bit is set to 1, yielding an address beginning with 0xfd. Then
|
|
|
|
* the network ID is filled into the global ID, subnet ID, and first byte
|
|
|
|
* of the "interface ID" field. Since the first 40 bits of the network ID
|
|
|
|
* is the unique ZeroTier address of its controller, this makes a very
|
|
|
|
* good random global ID. Since network IDs have 24 more bits, we let it
|
|
|
|
* overflow into the interface ID.
|
|
|
|
*
|
|
|
|
* After that we pad with two bytes: 0x99, 0x93, namely the default ZeroTier
|
|
|
|
* port in hex.
|
|
|
|
*
|
|
|
|
* Finally we fill the remaining 40 bits of the interface ID field with
|
|
|
|
* the 40-bit unique ZeroTier device ID of the network member.
|
|
|
|
*
|
|
|
|
* This yields a valid RFC4193 address with a random global ID, a
|
|
|
|
* meaningful subnet ID, and a unique interface ID, all mappable back onto
|
|
|
|
* ZeroTier space.
|
|
|
|
*
|
|
|
|
* This in turn could allow us, on networks numbered this way, to emulate
|
|
|
|
* IPv6 NDP and eliminate all multicast. This could be beneficial for
|
|
|
|
* small devices and huge networks, e.g. IoT applications.
|
|
|
|
*
|
|
|
|
* The returned address is given an odd prefix length of /88, since within
|
|
|
|
* a given network only the last 40 bits (device ID) are variable. This
|
|
|
|
* is a bit unusual but as far as we know should not cause any problems with
|
|
|
|
* any non-braindead IPv6 stack.
|
|
|
|
*
|
|
|
|
* @param nwid 64-bit network ID
|
|
|
|
* @param zeroTierAddress 40-bit device address (in least significant 40 bits, highest 24 bits ignored)
|
|
|
|
* @return IPv6 private unicast address with /88 netmask
|
|
|
|
*/
|
2016-06-24 13:40:50 +00:00
|
|
|
static InetAddress makeIpv6rfc4193(uint64_t nwid,uint64_t zeroTierAddress);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute a private IPv6 "6plane" unicast address from network ID and ZeroTier address
|
|
|
|
*/
|
|
|
|
static InetAddress makeIpv66plane(uint64_t nwid,uint64_t zeroTierAddress);
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|